Question

Objectives: Learn how to use the C++ while and for loops. Instructions: A perfect number is...

Objectives: Learn how to use the C++ while and for loops. Instructions: A perfect number is a positive integer greater than 1 that is equal to the sum of the positive integer numbers, including 1 but not including itself, that evenly divide into it. First, you should prompt the user for a number. Next, you should indicate if the number is a perfect number or not. Finally, you should list the numbers that evenly divide into the number entered and the sum of these numbers as an equation. Afterwards, start the process again by prompting the user for another number. You should stop the execution when the user enters a number that is not greater than 1. You are required to use at least one while loop and at least one for loop. Below are examples of perfect and imperfect numbers.

6 is a perfect number: 1+2+3=6

10 is not a perfect number: 1+2+5=10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

// Example program

#include <iostream>

#include <string>

using namespace std;

int main()

{

int factors[100];//to hold factors of a number

int count;//to count no of factors

int number;//to hold number

cout<<"Enter number";//asking user to enter number

cin>>number;//reading into number

while(number>1)//will continue till user enters number greater than 1

{

int sum=0;//to hold sum of factors

for(int i=1;i<=number/2;i++)//checking for factors

{

if(number%i==0)

{

sum+=i;//adding to sum

factors[count++]=i;//adding to factors

}

}

if(sum==number)//checking and printing results

{

cout<<number<<" is a perfect number:";

}

else

{

cout<<number<<" is not a perfect number:";

}

for(int i=0;i<count;i++)

{

if(i!=count-1)

cout<<factors[i]<<"+";

else

cout<<factors[i]<<"="<<number<<"\n";

}

count=0;

sum=0;

cout<<"Enter number";

cin>>number;

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Objectives: Learn how to use the C++ while and for loops. Instructions: A perfect number is...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

  • **Using C Language** Get a number from the user and use that number for loops and...

    **Using C Language** Get a number from the user and use that number for loops and to make some calculations. Get a number from the user (n) (For example if the user enters a 3, each loop below will repeat 3 times) //ask, get, and return an integer (by reference) void GetIntPointer(int *numPtr); //input: the number entered by the user (pass by copy) //Calculates the product of the first (n)numbers using a while loop and store the result in *productPtr...

  • Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of...

    Hi please help me with this zybooks question asap. Thanks 4.17 Chapter 4 Program: Sum of Numbers/C++ program Write a C ++ program that asks the user for a positive integer value by prompting "Enter a positive integer number: ", read in that number, then use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2,3, 4…50....

  • Use Python and only while loops For this part of the homework you will write code...

    Use Python and only while loops For this part of the homework you will write code to draw an isosceles right triangle. (A right triangle in which the height and base are the same size.) Your program should prompt the user for these inputs, in exactly this order: 1. The height of their triangle 2. The symbol the triangle will be outlined in 3. The symbol the triangle will be filled with For these inputs, you can assume the following:...

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT