Peterson Number in JavaIn 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 NumberA 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
Example of Peterson NumberSuppose, 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 ProgramIn 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.
Next TopicSunny Number in Java
|