Special Number

A number is known as Special number when sum of the factorial of digits is equal to the original number. It is also known as Krishnamurthy number. For example 145
1!+4!+5!= 1+24+120= 145

For the factorial program click the link
https://java4school.com/factorial-of-a-number

Write a program to accept a number and print whether it is a special number or not.

import java.io.*; 
class SpecialNumber //one when the sum of factorial of its digits is = the number itself e.g., 145 
{ 
    public static void main()throws IOException { 
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
        System.out.println("Enter a number"); 
        int n=Integer.parseInt(br.readLine()); 
        int fact=1; 
        int num=n; 
        int sum=0; 
        while(n!=0) { 
            int rem=n%10; 
            for(int i=1;i<=rem;i++) 
            { 
                fact*=i; 
            } 
            sum+=fact; 
            fact=1; 
            n=n/10; 
        } 
        if(num==sum) 
        { 
            System.out.println("It is a special number"); 
        } 
        else 
        { 
            System.out.println("Its not a special number"); 
        } } } 

Output:
Enter a number
145
It is a special number

For videos regarding java programming watch the video
https://www.youtube.com/watch?v=QZKcULOC4lg

For more number programs click the given link:
https://java4school.com/number-programs-part-4-icse-computer-application

LEAVE A REPLY

Please enter your comment!
Please enter your name here