C program to check whether a number is perfect number or not
Posted by Samath
Last Updated: January 02, 2017
  479

Write a C program to enter any number and check whether the number is Perfect number or not. 
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3 
And 1+2+3 = 6.

Code:

#include<stdio.h>
 
int main() {
   int num, i = 1, sum = 0;
 
   printf("Enter a number: ");
   scanf("%d", &num);
 
   while (i < num) {
      if (num % i == 0) {
         sum = sum + i;
      }
      i++;
   }
 
   if (sum == num)
      printf("%d is a Perfect Number", i);
   else
      printf("%d is Non Perfect Number", i);
 
   return 0;
}