In: Computer Science
Write a C++ program to allow a user to enter in any positive number greater than or equal to zero. The program should not continue until the user has entered valid input. Once valid input has been entered the application will determine if the number is an abundant number or not and display whether or not the number is an abundant number. If the user enters in a 0 the program should quit.
An abundant number is a number n for which the sum of divisors is greater than two times itself (i.e., >2n).
Consider 12: the divisors = ( 1,2 ,3,4,6,12)
1+2+3+4+6+12 = 25 > 2(12) = 25 > 24 (Yes 12 is abundant)
Consider 6: the divisors = ( 1,2 ,3, 6)
1+2+3+6 = 12 > 2(6) = 12 > 12 (No 6 is NOT abundant)
Make sure your program conforms to the following
requirements:
1. Keep running until the user enters a 0. (5 points)
2. Make sure the number is positive (at least 0). (5 points).
3. Determine if the number is abundant or not and print a message
to the user. (35 points)
4. Add comments wherever necessary. (5 points)
Sample Runs:
NOTE: not all possible runs are shown below.
Sample Run 1
Welcome to the abundant numbers program
Please enter in an number (>=0)...6
6 is NOT an abundant number
Please enter in an number (>=0)...12
12 is an abundant number
Please enter in an number (>=0)...-8
Please enter in an number (>=0)...24
24 is an abundant number
Please enter in an number (>=0)...45
45 is NOT an abundant number
Please enter in an number (>=0)...0
Process finished with exit code 0
Sample Run 2
Welcome to the abundant numbers program
Please enter in an number (>=0)...-8
Please enter in an number (>=0)...-12
Please enter in an number (>=0)...9
9 is NOT an abundant number
Please enter in an number (>=0)...0
Process finished with exit code 0
General Requirements:
1. No global variables (variables outside of main() )
2.Please make sure you;re only using the concepts already discussed in class. That is, please try and restrict yourself to input/output statements, variables, selection statements and loops. Using any other concept (like functions or arrays) will result in loss of points.
3. All input and output must be done with streams, using the library iostream
4. You may only use the iostream and iomanip libraries (you do not need any others for these tasks)
5. NO C style printing is permitted. (Aka, don’t use printf). Use cout if you need to print to the screen.
6. When you write source code, it should be readable and well-documented (comments).
C++ Program :
#include <iostream>
using namespace std;
int main()
{
int sum; // the sum of the divisors
int n; // number entered by the user
int i=0; // used in for loop to find divisors
//output welcome message
cout<<"Welcome to the abundant numbers program\n";
//while loop to keep the program running until user enters 0
while(1)
{
sum=0; // Initializing sum to 0 (the sum of divisors is set to
0
cout<<"Please enter in an number (>=0)...";
cin>>n;
if(n==0) //if entered number is equal to 0 exit
{
exit(0);
}
else if (n<0) // if entered number is less than 0 then skip the
current iteration(continue)
{
continue;
}
else
{
for(i=1;i<=n;i++) // loop to find the sum of divisors
{
if(n%i == 0) // if the value of i is divisible by entered
number
{
sum=sum+i; // add the value of i to sum
}
}
if(sum>(2*n)) // if the sum is greater than two times of the
entered number
{
cout<<n<<" is an abundant number\n";
}
else
{
cout<<n<<" is NOT an abundant number\n";
}
}
}
}
Output:
Note for Turbo C++ :
- Use getch(); in the end of the main loop
- use #include<iostream.h> instead of '#include<iostream>'
- use #include<conio.h> instead of 'using namespace std'
Please let me know in the comments if u have any queries ! Also consider giving a thumbs up:)