C program to find number is perfect or not

If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

 #include<stdio.h>
main()
{
 int num,i,sum=0;

//Accepting number
 printf("Enter the number\n");
 scanf("%d",&num);

//Check perfect no or not
for(i=1;i<=num-1;i++)
{
 if(num%i==0)
 {
  sum=sum+i;
 }
 else
 {
  sum=sum;
 }
}
 if(sum==num)
  printf("%d is perfect number\n",num);
 else
  printf("%d is not a perfect number\n",num);
}

OUTPUT:

Enter the number
4
4 is not a perfect number

Enter the number
6
6 is perfect number

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.