Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to write a program perfectNumber.c that asks user repeatedly for a n

ID: 2247892 • Letter: I

Question

I'm trying to write a program perfectNumber.c that asks user repeatedly for a non-negative integer, and stops when the number is zero. For each positive integer entered, your program is to check whether it is a perfect number or not.

Your program should have a function is_perfect(int) that returns 1 if the argument is a perfect number, or 0 otherwise.

Whats wrong with my code?

/** k x 5 #include # include int is perfect (int); 7 int main (void) { int num; printf("Enter number: ") scanf ("%d" , #) ; if (is perfect (num)1) ( 10 12 13 printf("%d is a perfect number. ",num); 15 16 17 18 19 L 20 int IS perfect (int num) { 21 (is perfect (num) printf("%d is not a perfect number. ",num); if { return 0; int perfect_number; int pp 1; perfectnumber-(pow2, (pp-1))) * ( (pow (, pp))-1): while (pp >= 1) { 23 25 26 27 28 29 30 31 32 if (perfect_number num) break; return 0; else if (perfect numbernum) [ return 1; 3 4 35 36 37 L return is perfect (num);

Explanation / Answer

#include<stdio.h>


int is_perfect(int);

int main(){
int num;
printf("Enter number: ");
scanf("%d",&num);
while(num!=0){ //exit condition,when input is 0
is_perfect(num); //function call
printf("Enter number: ");
scanf("%d",&num);
}
return 0;
}

int is_perfect(int num){

int sum=0,i;
//perfect number calculation

for(i=1;i<num;i++){
if(num%i==0) // check reminder equals to zero or not
sum=sum+i; //sum the divisors
}
if(sum==num) //check sum equals to number
printf("1(%d is a perfect number.) ",num);//return 1 when the no.is a perfect number
else
printf("0(%d is not a perfect number.) ",num); //return 0 when the no.is a not perfect number

return 0;
}

Definition of perfect number - A perfect number is a positive number where the sum of all divisors is equal to this number(excluding this number ).
example- 6 divisors- 1,2,3 sum=1+2+3=6 So, 6 is the perfect number.
for 8, divisors-1,2,4 sum=1+2+4=7 S0, 8 is not the perfect number.

In line no. 23 your perfect number calculation method is wrong.

Method is like -

for(i=1;i<num;i++){   
if(num%i==0)
sum=sum+i;
}

where num= input number and sum=calculate the sum of the divisor   

When you ask for repeatedly input from user the function should be like, that put the printf and scanf statement in loop

while(num!=0){
is_perfect(num);
printf("Enter number: ");
scanf("%d",&num);
}
return 0;
}