Check if a number is perfect number - C Program

Write a C program to accept a number and check whether it is perfect or not.(using function)

Solution:

Perfect Number - When sum of all positive divisors excluding that number is equal to that number.
Ex. 6 is the smallest perfect number.
Divisor of 6 are 1, 2, 3. Sum of divisors is : 1+2+3 = 6.

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


Output:

perfect number