An integer number is said to be a perfect number if its factors, including 1 (bu
ID: 3931187 • Letter: A
Question
An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines whether parameter number is a perfect number and another function list_perfect that prints its factors. Use this function in a program (main) that determines and prints all the perfect numbers between 1 and 10000. Starter code is provided along with this homework. Add perfect.c to your project and modify to get the following output.
/*Your header here */
#include <stdio.h>
int perfect(int);
void list_perfect(int);
int main()
{
int i;
for (i=2; i<10000; i++) {
if (perfect(i)) {
list_perfect(i); }
}
return 0;
}
/*Returns whether a given number is perfect*/int perfect(int number){
}
/*Lists the perfect number's factorsExample: 6 = 1 + 2 + 3*/void list_perfect(int number){
}
Explanation / Answer
#include <stdio.h>
int perfectOrNot(int number);
int main(void) {
int i,k=0;
int perfectNumberCount=0;
int perfectArray[10];
for(i=1;i<=10000;i++){
if(perfectOrNot(i))
perfectArray[k++]=i;
perfectNumberCount+=perfectOrNot(i);
}
printf("PERFECTCOUNT IS: %d ",perfectNumberCount);
for(k=0;k<4;k++)
printf("%d is a perfect number. ",perfectArray[k]);
return 0;
}
int perfectOrNot(int number){
int i,sum=0,perfectCount=0;
for(i=1;i<=(number/2);i++){ //No need to divide to numbers further than half of it
if(!(number%i)){
printf("%d can be divisible by %d. ",number,i);
sum=sum+i;
}
}
if (number==sum){
printf ("%d is %d. PERFECT number ",number,perfectCount);
return 1;
}
else {
printf ("%d is not a perfect number. ",number);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.