BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Peterson Number Program in Java

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial we will write a java program to check whether a given number is Peterson number or not. A number is called Peterson if the sum of factorials of each digit is equal to the number itself.

Peterson Number

As shown in the above image, number 145 is Peterson number as the sum of factorials of each digit of this number is equal to the number itself.

Let’s take another example, number 555 is not a Peterson number as the sum of factorials of each digit is 360 which is not equal to the 555.

5! + 5! + 5! = 120 + 120 + 120
             = 360

Program to check for Peterson number

In this example we are taking input from the user. User enters the number and then this program checks whether the input number is Peterson or not.

The logic we have used here is pretty simple, we already know that the single digit of any number ranges from 0 to 9 so we have stored the factorial of these numbers in an array. This way we can quickly finds the factorial of a digit by referring to this array. For example value of factorial[4] contains the factorial of 4.

The entered number is passed to a user-defined function isPeterson that we have created to check the number.

Inside the while loop, we are finding the factorial of last digit of the number at each iteration and at the end of the iteration we are removing that digit from the number so in the next iteration, we find the factorial of second last digit. This goes on and on until the factorials of all digits are found and added together in the variable sum.

At last the function compares the sum of factorials to the number itself and returns true or false.

import java.util.Scanner;
public class JavaExample
{
    /* This array is to determine the factorial of single digit quickly
     * (factorials of 0 to 9 stored)
     */
    static long[] factorial = new long[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter the number: ");

        //storing the input number in the variable num
        int num = scan.nextInt();

        /* now we are calling our user-defined function to check whether
         * the input number is Peterson or not
         */
        if (isPeterson(num))
            System.out.println(num + " is a Peterson number.");
        else
            System.out.println(num + " is not a Peterson number.");
    }

    //user-defined function where we have written the logic to check
    static boolean isPeterson(int n)
    {
        int num = n;
        int sum = 0;
        //looping the number until we find the factorial of each digit
        while (n > 0)
        {
            //we get the last digit of the number
            int lastDigit = n % 10;

            /* Since we have already stored the factorial of each digit 
             * in the array factorial, we are pulling the factorial quickly 
             * from the array itself by passing the digit
             */
            sum += factorial[lastDigit];

            /* Since we have stored the factorial of last digit in the 
             * variable sum, lets remove this digit from the number to find 
             * the factorial of second last digit in the second iteration of loop.
             */
            n = n / 10;
        }
        /* We are comparing the sum of factorials of all digits to the 
         * number and returning boolean value true or false.
         */
        return (sum == num);
    }
}

Output 1: When user entered the number 145

Enter the number: 145
145 is a Peterson number.

Output 2: When user entered the number 456

Enter the number: 456
456 is not a Peterson number.

To learn Java from scratch refer: Java tutorial for complete beginners.

Related Java Examples

  • Java program to find factorial of a given number using recursion
  • Java Program to Find Factorial using For and While loop
  • Java Program to add two binary numbers
  • Java Program to check if a number is perfect square

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap