Question

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 to 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). (5 points)
2. Make sure the number is positive (at least 1). (5 points).
3. Go from 1 to to the upper limit and print all numbers that are perfect. (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 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

General Requirements:
1. No global variables (variables outside of main() )

2.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 cmath, 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).

Homework Answers

Answer #1

// do comment if any problem arises

//code

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

    int upper_value;

    // welcome message

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

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

    // read upper value from user

    cin >> upper_value;

    // read upper value till user enters positive number

    while (upper_value <= 0)

    {

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

        cin >> upper_value;

    }

    cout << "The following are perfect numbers...\n";

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

    {

        int sum = 0;

        // calculate sum of its devisors

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

        {

            if (number % j == 0)

                sum += j;

        }

        // if sum is equal to number itself then number is perfect

        if (sum == number)

            cout << number << endl;

    }

}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Write a program (C language) that will read the number of a month and will print...
Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February. Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program. Program must meet the following criteria: 1.Your name and the name of the program...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum...
Please write C++ program. Fibonacci numbers have the property that each consecutive number is a sum of two preceding: 0, 1, 1, 2, 3, 5, 8, 13, 21 ..... Your program should ask user for the integer, and output the result, which shows Fibonacci number at that position. For example if user enters '5', program should output '3' (fifth position in the series). Your program should use recursive function to compute and return back to the main the Fibonacci number....
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number...
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number equal to the sum of its divisors - 6 and 28 are the first two! 6 is 2 +3 + 1 and 28 is 14+7+4+2+1). Being the perfect number is quite unique. Only 4 perfect numbers exist between 1 and 10,000 and you’ve already found two! Please write a program to find the additional pair. Bonus: How many perfect numbers exist between 10,000 and...
C programming language. Write a program to print reverse of all the numbers from m to...
C programming language. Write a program to print reverse of all the numbers from m to n using functions with arguments and no return type. The inputs in the program will be m and n variables and need to take these variables as arguments.
Write a C program that when you type in five numbers, it will print the number...
Write a C program that when you type in five numbers, it will print the number from the smallest one to the largest one.(if you type in 2, 1, 3, 5, 4, the output will be 1, 2, 3, 4, 5 ) You may need more than one function to complete this mission.
write a C++ program that display a prime numbers between 1 and 100 number. Plase print...
write a C++ program that display a prime numbers between 1 and 100 number. Plase print the remaining primes by “dots” For gexample The output should appear like The prime numbers between 1 and 100 are: 1, 2, 3, 5, 7, .........
1) Write a C program to print the prime numbers from 1 to 100. (A prime...
1) Write a C program to print the prime numbers from 1 to 100. (A prime integer is any integer that can be divided evenly only by itself and 1) Requirement: Use an array to take the number from 1 to 100 and another array to take the prime number.
An integer number is said to be a perfect number if it is equal to the...
An integer number is said to be a perfect number if it is equal to the sum of its factors (divisors), including 1 (but not the number itself). For example, 6 is a perfect number because 6 = 3+2+1. Write a method called isPerfect that returns true if the input integer number is a perfect number and false otherwise. Then, call this method in the main method that determines and prints all the perfect numbers between 2 and 2000. WRITE...
Write a C++ program to prompt a user for a positive integer and print the number...
Write a C++ program to prompt a user for a positive integer and print the number of even and odd digits (0 is even). Sample run: Enter a positive integer: -4580 Invalid. Enter a positive integer: 12146 2 odd digits 3 even digits