This is a c program to check whether a given number is a perfect number or not. But, what is a perfect number? If the sum of all proper divisors of a number is equal to the number itself, it is a perfect number. Proper divisors of a number means all its divisors except the number itself. A perfect number is a positive integer which is equal to sum of its proper divisors. An example is 6. 6 = 1 + 2 + 3. 28 is also an example for perfect number (1+2+4+7+14=28).
The following program checks a given number and tells whether it is a perfect number or not.
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("Enter a number:");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
No comments:
Post a Comment