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
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...
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
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to...
Write a program in C that sorts 3 given numbers, defined as (a,b,c) from highest to lowest. I already have this code finding the maximum. Need to just add the sorting code onto it. int A, B, C; A = 4; B=3; C=7; Max = A If (B > max) { Max = B; } Else if (c > max) { Max = c; }
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT