Question

In: Computer Science

Write a C++ program to allow a user to enter in any positive number greater than...

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).

Solutions

Expert Solution

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:)


Related Solutions

Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they...
Write a Python Program Continue to ask the user to enter a POSITIVE number until they type DONE to quit. The program should DOUBLE each of the digits in the number and display the original entry AND the SUM of the doubled digits. Hint: Use the // and % operators to accomplish this. Print the COUNT of entries that were invalid Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46. User Entry Output...
JAVA Write a java program that will sum all positive number. Prompt the user to enter...
JAVA Write a java program that will sum all positive number. Prompt the user to enter numbers and add all positive numbers. The program will not add negative numbers and the program will stop when you enter ‘0’.   Enter a number> 25 Enter a number> 9 Enter a number> 5 Enter a number> -3 Enter a number> 0 Sum = 39
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT