Source Code:
/* Write a c program to check whether a given number is a perfect number or not. (Perfect number is a positive number which sum of all positive divisors excluding that number is equal to that number. For example 6 is perfect number since divisor of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 = 6) */
#include <stdio.h>
int main()
{
int n,sum; /*n to store the number, sum - to store the sum of factors*/
printf("Enter a number: ");
scanf("%d",&n); /*Input the number from the test case 6, 28 & 45 respectively */
int i;
sum=0;
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("YES");
else
printf("NO");
return 0;
}
Output:
No comments:
Post a Comment