‘C’ program to check if a number is perfect
C File =>
/* Write a ‘C’ program to check if a number is perfect (number = sum of its factors) */
#include <stdio.h>
int main()
{
int n, i,sum=0;
printf("\n Enter a positive integer: ");
scanf("%d",&n);
printf("\n Factors of %d are: ", n);
for (i = 1; i<n; ++i)
{
if (n % i == 0)
{
printf("%d\t", i);
sum=sum+i;
}
} //end for
if(sum==n)
printf("\n %d is perfect number",n);
else
printf("\n %d is Not a perfect number",n);
return 0;
}
Comments
Post a Comment
Please do not enter any spam link in message.