# Java Program to Check Peterson Number

Write a Java program to check Peterson number or not. Any number can be Peterson if the sum of factorials of each digit is equal to the actual number. Here, for(int temp = pnum; temp > 0; temp = temp/10) is to divide the number into individual digits. Next, for(int i = 1; i <= rem; i++) loop is to find the factorial of those individual digits. The if statement if (sum == pnum) checks whether the sum equals the actual number, and if it is true, it is a Peterson number.

```package NumPrograms;

import java.util.Scanner;

public class PetersonNumber1 {
private static Scanner sc;

public static void main(String[] args) {
int fact, rem, sum = 0;
sc = new Scanner(System.in);

System.out.print("Please Enter Number to Check Peterson Num = ");
int pnum = sc.nextInt();

for(int temp = pnum; temp > 0; temp = temp/10)
{
fact = 1;
rem = temp % 10;

for(int i = 1; i <= rem; i++)
{
fact = fact * i;
}
sum = sum + fact;
}

if (sum == pnum)
{
System.out.println(pnum +  " is a Peterson Number");
}
else
{
System.out.println(pnum +  " is Not a Peterson Number");
}
}

}```

This Java example checks whether the given number is Peterson or not using recursive functions. The calFactorial function will find the factorial of each digit, and the findPetersonNum function will divide the number into digits and find its factorial sum.

```package NumPrograms;

import java.util.Scanner;

public class PetersonNumber2 {
private static Scanner sc;

public static void main(String[] args) {

sc = new Scanner(System.in);

System.out.print("Please Enter Number to Check Peterson Num = ");
int pnum = sc.nextInt();

int sum = findPetersonNum(pnum);

if (sum == pnum)
{
System.out.println(pnum +  " is a Peterson Number");
}
else
{
System.out.println(pnum +  " is Not a Peterson Number");
}
}

public static int calFactorial(int pnum) {

if(pnum == 0 || pnum == 1)
{
return 1;
}
else
{
return pnum * calFactorial(pnum - 1);
}
}

public static int findPetersonNum(int pnum) {
int fact, rem, sum = 0;

for(; pnum > 0; pnum = pnum/10)
{
fact = 1;
rem = pnum % 10;
fact = calFactorial(rem);
System.out.println("Factorial of " + rem + " = " + calFactorial(rem));
sum = sum + calFactorial(rem);;
}
return sum;
}
}```
``````Please Enter Number to Check Peterson Num = 145
Factorial of 5 = 120
Factorial of 4 = 24
Factorial of 1 = 1
145 is a Peterson Number``````

## Java Program to Check Peterson Number using a while loop

```package NumPrograms;

import java.util.Scanner;

public class PetersonNumber3 {
private static Scanner sc;

public static void main(String[] args) {

sc = new Scanner(System.in);

System.out.print("Please Enter Number to Check Peterson Num = ");
int pnum = sc.nextInt();

int sum = findPetersonNum(pnum);

if (sum == pnum)
{
System.out.println(pnum +  " is a Peterson Number");
}
else
{
System.out.println(pnum +  " is Not a Peterson Number");
}
}

public static int calFactorial(int pnum) {

if(pnum == 0 || pnum == 1)
{
return 1;
}

return pnum * calFactorial(pnum - 1);
}

public static int findPetersonNum(int pnum) {
int rem, sum = 0;

while(pnum > 0)
{
rem = pnum % 10;
sum = sum + calFactorial(rem);
pnum = pnum/10;
}
return sum;
}
}```
``````Please Enter Number to Check Peterson Num = 40585
40585 is a Peterson Number

Please Enter Number to Check Peterson Num = 125
125 is Not a Peterson Number``````

This Java program prints the list of Peterson numbers between 1 to N or 1000.

```package NumPrograms;

import java.util.Scanner;

public class PetersonNumber4 {
private static Scanner sc;

public static void main(String[] args) {

sc = new Scanner(System.in);

System.out.print("Enter Perterson Numbers Start and End Range = ");
int start = sc.nextInt();
int end = sc.nextInt();

System.out.println("The List of Peterson Numbers from " + start + " to " + end);
for(int i = start; i <= end; i++)
{
if (findPetersonNum(i) == i)
{
System.out.println(i + " is a Peterson Number");
}
}
}

public static int calFactorial(int pnum) {

if(pnum == 0 || pnum == 1)
{
return 1;
}

return pnum * calFactorial(pnum - 1);
}

public static int findPetersonNum(int pnum) {
int rem, sum = 0;

for(;pnum > 0; pnum = pnum/10)
{
rem = pnum % 10;
sum = sum + calFactorial(rem);
}
return sum;
}
}```
``````Enter Perterson Numbers Start and End Range = 2 10000
The List of Peterson Numbers from 2 to 10000
2 is a Peterson Number
145 is a Peterson Number``````