C Program to Check Perfect Number

A perfect number is a positive integer number in which sum of all positive divisors excluding the number itself is equal to that number.

For example: 28 is perfect number since its divisors are 1, 2, 4, 7 and 14. Sum of divisors is: 1+2+4+7+14=28.

Other examples of perfect number are: 6, 496 etc.

Perfect Number C Program


#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
	 int number, sum=0, i;
	 clrscr();
	 printf("Enter number: ");
	 scanf("%d", &number);
	
	 /* Finding sum */
	 for(i=1; i< number; i++)
	 {
	  if(number%i==0)
	  {
	   sum = sum +i;
	  }
	 }
	 /* Making decision */
	 if(sum == number)
	 {
	  printf("%d is PERFECT.", number);
	 }
	 else
	 {
	  printf("%d is NOT PERFECT.", number);
	 }
	 getch();
	 
	 return(0);
}

Output of the above program :

Run 1:
-------------
Enter number: 6 ↲
6 is PERFECT.

Run 2:
-------------
Enter number: 36 ↲
36 is NOT PERFECT.

Run 3:
-------------
Enter number: 496 ↲
496 is PERFECT.

Note: ↲ indicates enter is pressed.