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
C# Question
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.


VulpesRank #55 · 29.2K reputationPosted Aug 9, 2013, 2:45 AM EDT
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();
}
}
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 9, 2013, 7:28 AM EDT
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();
}
Nilesh Sanyal191 reputationPosted Aug 9, 2013, 7:46 PM EDT