Number is a perfect number or not
C program to check whether a given number is a perfect number or not.
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
For example, num = 28
Divisors of 28: 1, 2, 4, 7, 14, 28
Sum of divisors (excluding 28): 1 + 2 + 4 + 7 + 14 = 28
Sum = Original number
So, 28 is a perfect number
Some of the perfect numbers are 6, 28, 496, 8128, and 33550336, etc.
For example, num = 28
Divisors of 28: 1, 2, 4, 7, 14, 28
Sum of divisors (excluding 28): 1 + 2 + 4 + 7 + 14 = 28
Sum = Original number
So, 28 is a perfect number
Some of the perfect numbers are 6, 28, 496, 8128, and 33550336, etc.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num, i, sum;
printf("Enter the number\t:");
scanf("%d",&num);
sum = 0;
printf("The positive divisors are ");
for(i=1;i<num;i++)
{
if(num%i==0)
{
sum=sum+i;
printf("%d ",i);
}
}
printf("\nThe sum of the divisors is : %d",sum);
if(sum==num)
printf("\nSo, the number is perfect.");
else
printf("\nSo, the number is not perfect.");
getch();
}
Output
********** Run1 **********
Enter the number :56
The positive divisors are 1 2 4 7 8 14 28
The sum of the divisors is : 64
So, the number is not perfect.
********** Run2 **********
Enter the number :496
The positive divisors are 1 2 4 8 16 31 62 124 248
The sum of the divisors is : 496
So, the number is perfect.