Write a C program to check whether a given number (N) is a perfect number or not

Write a C program to check whether a given number (N) is a perfect number or not?

What is a perfect number?

Perfect Number - A perfect number is a positive integer number which equals to the sum of its proper positive divisors. For example, 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.


Now in this post, we will discuss about  Write a C program to check whether a given number (N) is a perfect number or not? , C program for checking whether a given number is perfect or not, C programing code for finding the perfect numbers, we will find whether a given number is perfect or not. 


You may also read :

At first we had initialize the main function then we have taken the input and stored in N.
Now we have to fetch the sum of the divisor of the input number. For that reason, we have declared a variable named "sum" to store the result in it. Initially, the value of sum =0 , Then we had used for loop to collect the sum of it's (here it refers the input number N) divisor.

Take for example if we give input N=8000. Then for it's first iteration i=1 and the condition of the for loop becomes true and inside the for loop, the condition
if(N%i==0) is also satisfied i.e 8000%1==0 is true and the value of sum becomes  (sum + i) i.e sum=0+1=1

Don't know how to use loop just goto this page:


Now for the second iteration, the value of i increases to 2 and again the condition of the for loop becomes true and inside the for loop, the condition if(N%i==0) is also satisfied i.e 8000%2==0 is true and the value of sum becomes  (sum + i) i.e  sum= 1+2=3

For the third iteration, the value of i increases to 3 and again the condition of the for loop becomes true and inside the for loop, the condition if(N%i==0) is not satisfied this time i.e 8000%3==0 is not true as it has the remainder 2. and the value of sum remains 3. 

Again for the fourth iteration, the value of i increases to 4 and again the condition of the for loop becomes true and inside the for loop, the condition if(N%i==0) is also satisfied i.e 8000%4==0 is true and the value of sum becomes  (sum + i) i.e  sum= 3+4=7

This process will continue until the value of i  becomes N. When the value of i becomes i.e i=8000 then the condition of the for loop will become false and the program control will exit the for loop and will execute the next statement i.e if(N==sum) and compare the value of sum with N i.e if sum=given input number then it will print 8000 is a perfect number  otherwise it will print 8000 is not a perfect number.
Hence the program ended.


This is the program code to check whether a given number (N) is a perfect number or not

#include <stdio.h>int main(){    int N;     scanf("%d",&N);      /* An integer number taken as input  */

int i,sum=0;for(i=1;i<N;i++) {  if(N%i==0)          sum=sum+i;
}if(N==sum)  printf("\n%d is a perfect number.",N);else  printf("\n%d is not a perfect number.",N);}
  



   Input                                                 Output
8000      
8000 is not a perfect number.

#Conclusion:

Hence the problem Write a C program to check whether a given number (N) is a perfect number or not? is solved.

Happy coding.

Post a Comment

0 Comments