Kodekraftt
/Blog
/Peterson Number In Java
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Peterson Number in Java

A Peterson Number is a number in which the sum of the factorials of its digits is equal to the original number.

Peterson Number in Java

A Peterson Number is a special number where the sum of the factorials of its digits is exactly equal to the number itself.

Example:
145 → 1! + 4! + 5! = 1 + 24 + 120 = 145
So, 145 is a Peterson Number.

Java Program:


public class PetersonNumber {

    // Function to calculate factorial of a digit
    static int factorial(int n) {
        int fact = 1;
        for (int i = 1; i <= n; i++) {
            fact *= i;
        }
        return fact;
    }

    // Function to check if a number is a Peterson Number
    static boolean isPeterson(int num) {
        int temp = num;
        int sum = 0;

        while (temp > 0) {
            int digit = temp % 10;
            sum += factorial(digit);
            temp /= 10;
        }

        return sum == num;
    }

    public static void main(String[] args) {
        int num = 145;

        if (isPeterson(num)) {
            System.out.println(num + " is a Peterson Number");
        } else {
            System.out.println(num + " is NOT a Peterson Number");
        }
    }
}

Practice Challenges

  1. Modify the program to print all Peterson Numbers between 1 and 1000.
  2. Take a number from user input and check if it's a Peterson Number.
  3. Optimize the program by precomputing factorials of digits 0–9.

You might also like

Disarium Number in Java: Definition, Examples, and Complete Program

A detailed explanation of Disarium Numbers in Java with definition, examples, step-by-step logic, and a complete Java program. Ideal for beginners, students, and coding interview preparation.

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

Fascinating Number in Java with Explanation, Definition, Examples, and Program

A Fascinating Number is a number which, when multiplied by 2 and 3 and then concatenated with itself, forms a string that contains all digits from 1 to 9 exactly once.

Check Perfect Square in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to check whether a number is a Perfect Square in Java without using Math.sqrt(). This guide explains the logic, provides examples, and includes an efficient program using iterative checking.

Automorphic Number in Java: Definition, Explanation, and Step-by-Step Program

A clear and detailed guide on Automorphic Numbers in Java, including definition, examples, logic breakdown, and a complete Java program. Ideal for beginners, students, and coding interview preparation.

Happy Number in Java

Learn what a Happy Number is and how to check whether a number is happy or not in Java. This guide explains the concept, algorithm, working process, dry run, and provides multiple Java programs for beginners.