Extra#1 Perfect number (due 11/1) Part a) (do not submit this part Write a progr
ID: 3601882 • Letter: E
Question
Extra#1 Perfect number (due 11/1)
Part a) (do not submit this part
Write a program to ask the user to enter an integer from 1 to 100 (validate user's input), and display the number together with all of its factors (except the number itself) and the sum of the factors. If the sum of the factors equals the number display <= is a PERFECT NUMBER (see below)
Sample RUN
Enter an integer from 1 to 100: 24
The factors of 24 are: 1, 2, 3, 4, 6, 8, 12 with a sum of 36
Sample RUN
Enter an integer from 1 to 100: 6
The factors of 6 are: 1, 2, 3 with a sum of 6 <= is a PERFECT NUMBER
Part b) (submit this part only)
Expand part a) to check all integers from 1 to 500 to display all the perfect numbers (there are a total of 3).
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
/* We are starting from 2 as 1 is not the PERFECT NUMBER*/
int start = 2;
int end = 500;
int number = 0; // Hold the current integer between 2 and 500
int factors_sum = 0; // hold the sum of factors
int factor; // hold the value to check for the factor
/* This loop is Going one by one from start to end */
while(start <= end){
number = start;
factors_sum = 0;
/*We will start checking the numbers from
the half of the entered value*/
factor = number / 2 ;
/*This loop will go to each integer
less than half of the given interge number*/
while(factor > 1){
/* checking the divisibility */
if( (number%factor) == 0 ){
factors_sum += factor;
}
factor--;
}
/* checking for PERFECT NUMBER */
if( ( factors_sum + 1) == number)
cout<<number<<" ";
start++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.