C program to check if number is Perfect number or not

C program to check if number is Perfect number or not


Explanation:


A number is said to be perfect number if sum of its proper divisors is equal to number itself.
e.g: 28
Proper divisors = 1,2,4,7,14
Sum of proper divisors = 1 + 2 + 4 + 7 + 14 = 28
So, 28 is a perfect number.

Program:

#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("\nEnter number to be checked: ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
                    sum=sum+i;
}
}
if(sum==n)
{
printf("\nIt is a Perfect Number");
}
else
{
printf("\nIt is NOT a Perfect Number");
}
return 0;
}

Output:







No comments:

Post a Comment