sum
of factorials
of its digits in a given base. 145
is a Peterson number as 1! + 4! + 5! = 1 + 24 + 120 = 145
. Here's a Java program to check if a number is a Peterson number or not :import java.util.Scanner;
public class PetersonNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
int temp = n, sum = 0;
while (temp > 0) {
int digit = temp % 10;
sum += factorial(digit);
temp /= 10;
}
if (sum == n) {
System.out.println(n + " is a Peterson number.");
} else {
System.out.println(n + " is not a Peterson number.");
}
}
public static int factorial(int n) {
int fact = 1;
for (int i = 2; i <= n; i++) {
fact *= i;
}
return fact;
}
}
Enter a number: 145
145 is a Peterson number.
Enter a number: 123
123 is not a Peterson number.
Scanner
class. factorial()
method is used to find the factorial of a given number.