# Peterson Number in Java

In this section, we will learn what is Peterson number and how can we check whether a given number is Peterson or not through a Java program.

## Peterson Number

A number is said to be Peterson if the sum of factorials of each digit is equal to the sum of the number itself.

### Steps to Find Peterson Number

• Read or initialize a number (n).
• Find the last digit (d) of the given number.
• Find the factorial (fact) of the digit.
• Add the factorial (fact) to a variable
• Repeat steps 2 to 4 until the given number becomes 0.
• Compare the sum with n. If both are equal, the given number is Peterson, else not.

### Example of Peterson Number

Suppose, we have to check the number (n) 145 is Peterson or not.

Number = 145

145 = !1 + !4 + !5

=1+4*3*2*1+5*4*3*2*1

=1+24+120

145=145

We observe that the number and the sum of factorials of digits are equal to the number itself. Hence, 145 is a Peterson number.

Let's implement the above logic in a Java program.

## Peterson Number Java Program

In the following program, we have taken an array of factorial to quickly find the factorial. You can use the logic also.

PetersonNumberExample.java

Output 1:

```Enter a number to check: 145
The given number is a Peterson number.
```

Output 2:

```Enter a number to check: 773
The given number is not a Peterson number.
```

### Feedback   