Special Number : Java : BlueJ

Objective :

The class SpecialNumber checks if the sum of the factorial of the digits of the Number is Equal to the number itself. This is termed to be a Special Number. For Example 2= 2! =2.
145 = 1! + 4! + 5! = 1+24+120 = 145. Find Out Others Yourself !

BlueJ Program Screenshot :




Java Program Source Code :

/**
 * The class SpecialNumber checks if the sum of the factorial of the digits of the
 * Number is Equal to the number itself. For Example 2= 2! =2.
 * 145 = 1! + 4! + 5! = 1+24+120 = 145. Find Out Others Yourself !
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
import java.io.*;
public class SpecialNumber
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    static int num;
    
    private void input()throws Exception
    {
        System.out.print("\nEnter the Number to check for Special Number : ");
        num=Integer.parseInt(br.readLine());
    }
    
    public boolean isSpecial(int n)
    {
        int spec=0,temp=n;
        while(temp%10!=0)
        {
            spec+=factorial(temp%10);
            temp/=10;
        }
        if(spec==n) return true;
        else    return false;
    }
    
    private int factorial(int n)
    {
        if((n==1)||(n==0))
            return 1;
        else
            return (n*factorial(n-1));
    }
    
    public static void main(String args[])throws Exception
    {
        SpecialNumber obj=new SpecialNumber();
        obj.input();
        if(obj.isSpecial(num))
            System.out.print(num+" is a Special Number");
        else    System.out.print(num+" is not a Special Number");
    }
}

    


© Shantanu Khan 0code ® Special Number