Last Updated: 16 April, 2023
On this tutorial page, we are going to learn how to write a Java program to check whether a number is peterson or not.
A Peterson number is a number that can be expressed as the sum of the factorials of its digits in a given base. In other words, if a number "n" in base "b" is equal to the sum of the factorials of its digits "d1, d2, ... dk", then it is called a Peterson number.
For example, 145 is a Peterson number in base 10, since 1! + 4! + 5! = 1 + 24 + 120 = 145.
Here's a Java program to check if a given number is a Peterson number:
Example: Check the given number is Peterson or not in Java
In the above program, taking user input using the java.util.Scanner class and passing the number as input in the isPeterson() method and checking whether the number is Peterson or not.
In the first execution, passing 145 as an input, which is a Peterson number,
In the second execution, 234 is passed as an input, which is not a Peterson number. Both the outputs are given below.
Output 1
Enter the number: 145
145 is a Peterson number.
Output 2
Enter the number: 234
234 is not a Peterson number.
That's all guys, hope this Java Program is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com