Program to check whether given number is a perfect number or not
A number is a perfect number if it is equal to sum of all of it's positive divisors excluding itself
| 1 |
/*Programme to check whether a given number is a perfect number or not*/ |
| 2 |
#include <stdio.h> |
| 3 |
#include <conio.h> |
| 4 |
void main() |
| 5 |
{ |
| 6 |
int num,i,sum=0; |
| 7 |
|
| 8 |
printf("Enter a Number to chech it's perfection and 0 to quit: "); |
| 9 |
scanf("%d",&num); |
| 10 |
|
| 11 |
/*If vlaue entered by user is greater than 0 then continue else exit*/ |
| 12 |
while(num!=0) |
| 13 |
{ |
| 14 |
/*Calculate the sum of all the positive divisors*/ |
| 15 |
for(i=1; i<num; i++) |
| 16 |
{ |
| 17 |
if(num%i==0) |
| 18 |
{ |
| 19 |
printf("%d + ",i); |
| 20 |
sum=sum+i; |
| 21 |
} |
| 22 |
} |
| 23 |
/*Check if the sum=num*/ |
| 24 |
if(num==sum) |
| 25 |
{ |
| 26 |
printf("= %d",sum); |
| 27 |
printf("Perfet Number"); |
| 28 |
} |
| 29 |
else |
| 30 |
{ |
| 31 |
printf("= %d",sum); |
| 32 |
printf("Not a perfet Number"); |
| 33 |
} |
| 34 |
sum=0; |
| 35 |
printf("Enter a Number to chech it's perfection and 0 to quit: "); |
| 36 |
scanf("%d",&num); |
| 37 |
} |
| 38 |
} |
| 39 |
|