Java program to check the Special number | JAVA POINT

Java Program for Special number


Write a Java program to check that a given number is Special number or not.


Special number

If the sum of the factorial of each digit of a number (N) is equal to the number itself, the number (N) is called a Special number or Peterson number or Krishnamurthy Number. 


PROGRAM :


import java.util.Scanner;

public class specialnum
{
 public static void main(String[] args)
 {
        
    int n, num, r,
    sum = 0;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the number to check : ");
    n = sc.nextInt();
    num = n;
    while (num > 0)
    {
      r = num % 10;
      int fact=1;
      for(int i=1;i<=r;i++)
      {
        fact=fact*i;
      }
      sum = sum+fact;
            num = num / 10;
    }
    if(n==sum)
    {
     System.out.println("Special Number" );
     }
     else
     {
       System.out.println("Not Special Number" );
        }
    }

}



/*

Output :


Enter the number to be checked : 145

( 145=1!+4!+5!=1+24+120=145 )

145 is a Special number.

*/