Pages

Wednesday, 24 June 2015

Peterson Number

C Program to check whether a number is a Peterson Number or not

Coding:
#include<stdio.h>
int main()
{
int n,i,no,sum=0,d,p;
printf("Please enter the number:");
scanf("%d",&n);
no=n;

n=no;
while(n>0)
{
d=n%10;
n=n/10;
for(i=1,p=1;i<=d;i++)
p=p*i;
sum=sum+p;
}
if(sum==no)
{
printf("%d is a Peterson number",no);
}
else
{
printf("%d not a Peterson number",no);
}
return 0;
}
Output:
PLease enter the number:145
145 is a Peterson number