Programming Examples
C program to check whether the given number is Perfect Number or not
Write a program to check whether the given number is Perfect Number or not
Perfect Number :- Perfect number, a positive integer equal to the sum of its proper divisors. The smallest Perfect number is 6, which is the sum of 1, 2 and 3.
For Example:-
Input = 28
Output = 28 is the Perfect Number.
So as we can see that the divisor of 28 is 1, 2, 4, 7, 14, so the sum of all the divisors is 28 (1 + 2 + 4 + 7 + 14 = 28). Therefore, 28 is the Perfect number.
Solution
#include <stdio.h>
int main()
{
int num,i;
int sum;
printf("Enter any Integer Number");
scanf("%d",&num);
sum=0;
for(i=1; i< num;i++)
{
if(num%i==0)
sum+=i;
}
if(sum==num)
printf("%d is a perfect number.",num);
else
printf("%d is not a perfect number.",num);
return 0;
}
Output
Enter any Number 28
28 is a perfect number.