Peterson Number Program in Java


package javaprograms.loop;

import java.util.Scanner;

class PetersonNumber
{

    static int fact(int number)
    {
        int f = 1;
        for(int i=1;i<=number;i++)
        {
            f = f*i;
        }
        return f;
    }

    public static void main(String[] args)
    {
        int n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number=");
        n = sc.nextInt();
        int sumOfFact = 0;
        int num = n;
        while (num != 0)
        {
            sumOfFact = sumOfFact + fact(num % 10);
            num = num / 10;
        }
        if (sumOfFact == n)
        {
            System.out.println(n + " is a Peterson number");
        } else
        {
            System.out.println(n + "is not a Peterson number");
        }
    }
}

Output:

Enter number=145
145 is a Peterson number
learn java study java