# Java Program to Check Peterson Number

In the previous article, we have discussed Java Program to Check Ugly Number

In this article we are going to understand what Peterson number is and how we can check whether a number is Peterson number or not in Java with examples.

## Program to Check Peterson Number

Peterson numbers are numbers whose factorial of individual digits adds up to the number itself. These are also known as strong number or Krishnamurthy number.

Example :

145: 1!+4!+5!= 1+24+120 = 145 Peterson number
10: 1!+0!=1+0=11 Not a Peterson number
127: 1!+2!+7!= 1+2+5040 =5043 Not a Peterson number

In the above examples the numbers 10 and 127 are not Peterson numbers as their factorials don’t add up to the numbers. Hence 145 is the only Peterson number here.

Let’s see different ways to check Peterson number.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach :

1. Enter/declare a number and store it .
2. We calculate the factorials of each digit using a function and add them.
3. If the resultant sum is the same as the entered number, then the number is said to be a Peterson number.

### Method-1:Java Program to Check Peterson Number By Using Static Value

import java.util.*;

public class PetersonNumber
{
public static void main(String args[])
{
//A number declared
int num = 145;

int temp = num,remainder, sum =0;
//Loop to iterate through digits and add their factorials
while(temp>0)
{
remainder = temp%10;
sum+= factorialOf(remainder);
temp = temp /10;
}

if(sum==num)
{
System.out.println(num+" is a Peterson number");
}
else
{
System.out.println(num+" is not a Peterson number");
}
}

// Function that returns the factorial of the number
static int factorialOf(int num)
{
int prod = 1;
while(num>0)
{
prod = prod*num;
num--;
}
return prod;
}
}

Output:

145 is a Peterson number

### Method-2: Java Program to Check Peterson Number By User Input Value

import java.util.*;

public class PetersonNumber
{
public static void main(String args[])
{
//Taking the number as input from the user using scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();

int temp = num,remainder, sum =0;
//Loop to iterate through digits and add their factorials
while(temp>0)
{
remainder = temp%10;
sum+= factorialOf(remainder);
temp = temp /10;
}

if(sum==num)
{
System.out.println(num+" is a Peterson number");
}
else
{
System.out.println(num+" is not a Peterson number");
}
}

// Function that returns the factorial of the number
static int factorialOf(int num)
{
int prod = 1;
while(num>0)
{
prod = prod*num;
num--;
}
return prod;
}
}

Output:

Case-1

Enter a number : 145
145 is a Peterson number

Case-2

Enter a number : 14
14 is a Peterson number

### Method-3: By C Programming Language

#include<stdio.h>
#include<math.h>
int main()
{
long int sum=0,f,temp,fact, number;
printf("Entre a Number:");
scanf("%ld", &number);
temp=number;
while(number>0)
{
for(f=number%10, fact=1;f>0;f--)
{
fact=f*fact;
}
sum+=fact;
number/=10;
}
if(sum==temp)
printf("Peterson number");
else
printf("Not a Peterson Number");
return 0;
}

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: