Monday, February 16, 2009

Write a C program to determine the given integer is Perfect number or not.

Perfect number:- Sum of the factorials of a individual digit is known as Perfect number
Ex:- 145 = 1! + 4! + 5!
=> 145 = 1 + 24 + 120
==> 145 = 145

So '145' is a Perfect number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, rem, sum=0, temp, fact;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
temp=no;
while(no>0)
{
fact=1;
for( rem= no%10 ; rem>=1; rem--) { fact=fact*rem; }
sum = sum+ fact;
no = no/10;
}
if(temp == sum)
printf("\n%d is Perfect number", temp);
else
printf("\n%d is not a Perfect number", temp);
getch();
}

Output:-
Enter the required number: 145

145 is Perfect number.

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

No comments:

Post a Comment