Please provide the c# codes to find if a number is peterson number or not. Peterson number means the sum of individual factorials of the number is equal to the
number itself,e.g 143 is a peterson no. because 1!+4!+5!=145
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 9, 2013, 6:45 AM
using System;
class Peterson
{
static void Main()
{
Console.Write("Enter a number : ");
int n = int.Parse(Console.ReadLine());
int temp = n;
int digit, fact = 1, sum = 0;
while(temp > 0)
{
digit = temp % 10;
for(int i = 1; i <= digit; i++) fact *= i;
sum += fact;
fact = 1;
temp /= 10;
}
if (n > 0 && n == sum)
{
Console.WriteLine("{0} is a Peterson number", n);
}
else
{
Console.WriteLine("{0} is not a Peterson number", n);
}
Console.ReadKey();
}
}
Nilesh SanyalPosted Aug 9, 2013, 11:46 PM
Anurag SarkarPosted Aug 9, 2013, 11:28 AM
void main()
{
int n,c,s=0,m,i,f=1;
Console.WriteLine("ENTER A VALUE : ");
int n=int.Parse(Console.Readline());
m=n;
while(n>o)
{
c=n%10;
for(i=0;i
f=f*i;
s=s+f;
f=1;
n=n/10;
}
if(s==m)
{
Console.WriteLine("\n THE ENTERED NUMBER IS PETERSON NUMBER.");
Console.ReadLine();
}
else
{
Console.WriteLine("\n THE ENTERED NUMBER IS NOT A PETERSON NUMBER."); Console.ReadLine();
}