Question

In: Computer Science

Write a C++ program to print all the perfect numbers below a certain given number. A...

Write a C++ program to print all the perfect numbers below a certain given number.

A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers.

6 = ( 1+2 +3)

28 = (1+2+4+7+14)

Make sure your program conforms to the following requirements:

1. Accept the upper limit from the user (as an integer).

2. Make sure the number is positive (at least 1).

3. Go from 1 to to the upper limit and print all numbers that are perfect.

Sample Runs:

NOTE: not all possible runs are shown below.

Sample Run 1

Welcome to the locate perfect numbers program...
What is the upper value?30
The following are perfect numbers...
6
28

Process finished with exit code 0

Sample Run 2

Welcome to the locate perfect numbers program...
What is the upper value?-8
What is the upper value? (must be an integer > 0)0
What is the upper value? (must be an integer > 0)500
The following are perfect numbers...
6
28
496

Solutions

Expert Solution

In case of any query, do comment. Please rate your answer. Thanks

Please use below code:

#include <iostream>

using namespace std;

int main()

{

    int upperValue = 0, sumOfDivisors=0;

    cout<<"Welcome to the locate perfect numbers program..."<<endl;

    cout<<"What is the upper value? " ;

   

    cin>> upperValue;

    //if upper value not in range, ask again to take input

    while(upperValue <= 0)

    {

        cout<<"What is the upper value? (must be an integer > 0) ";

        cin>> upperValue;

    }

  

    //Print the perfect numbers

    cout<<"The following are perfect numbers..."<<endl;

    //iterate through 1 to uppervalue

    for (int number = 1; number <= upperValue; number++) {

        //reset the sum of divisors to 0

        sumOfDivisors =0;

      

        for(int i=1; i<number; i++)

        {

            //find divisor of the number and add it to sumOfDivisors

            if(number % i == 0)

            {

                sumOfDivisors = sumOfDivisors + i;

            }

        }

       

        //after all divisors check if sumOfDivisors is equal to number then print the number

        if(sumOfDivisors == number)

            cout<< number<< endl;

    }

        

   

    return 0;

}

===================screen shot of the code=========================

Output:


Related Solutions

Java program to print all the powers of 2 below a certain number. Calculate sum, accept...
Java program to print all the powers of 2 below a certain number. Calculate sum, accept upper limit and make sure sum variable is long type.   Run- Enter the upper limit: 100 5 + 8 + 9 + 11 + 20 + 32 + 30 = 115
{ /* Write a program in C++ to find the perfect numbers between 1 and 500....
{ /* Write a program in C++ to find the perfect numbers between 1 and 500. The perfect numbers between 1 to 500 are: 6 28 496*/    int sum = 0;    int i = 1;    int j = 1;    for (i ; i <= 100; i++)    {        for (j; j <= 100; j++)        {            if (j < i)//Line 55            {                  ...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
Write a program in Python to print all possible combinations of phone numbers. The length of...
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Write a program in c++ that merges numbers from two files and writes all the numbers...
Write a program in c++ that merges numbers from two files and writes all the numbers into a third file in ascending order. Each input file contains a list of 50 sorted double floating-point numbers from the smallest to largest. After the program is run, the output file will contain all 100 numbers between in the two input files, also sorted from smallest to largest. Format the output into two columns – the first column contains the numbers 1-100, and...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT