Thursday, 21 August 2014

C Program to Check Whether Number is Perfect Or Not





A  perfect number is a positive integer that is equal to the sum of its proper positive divisor, that is, the sum of its positive divisors excluding the number itself . Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is1 + 2+ 3 =6and  28 is also a Perfect Number since 1+ 2 + 4 + 7 + 14= 28Other perfect numbers: 496, 8128

C program to check whether number is perfect or not.

#include<stdio.h>
int main()
{
int num, i = 1, sum = 0;
 printf("Enter a number: ");
 scanf("%d", &num);
 while (i < num)
{
if (num % i == 0)
{
sum = sum + i;
}
i++;
}
if (sum == num)
printf("%d is a Perfect Number", i);
else
printf("%d is Non Perfect Number", i);
return 0;
}
output :
Enter a number : 28
28 is a perfect Number.