Krishnamurthy Number in Java

 Krishnamurthy Number in Java

Question : Write a program to check whether a number is Krishnamurthy number or not?

Krishnamurthy Number : A number whose sum of the factorial of digits is equal to the number itself.

Examples :-

145
1! + 4! + 5! = (1 X 1) + (4 X 3 X 2 X 1) + (5 X 4 X 3 X 2 X 1) = 1 + 24 + 120 = 145
Output : It is a Krishnamurthy Number

55
5! + 5! = (5 X 4 X 3 X 2 X 1) + (5 X 4 X 3 X 2 X 1) = 120 + 120 = 240
Output : It is not a Krishnamurthy Number


Note : To understand the program clearly watch this video : Krishnamurthy Number in Java



Program to check Krishnamurthy Number:-

Program :-

import java.io.*;
import java.util.*;
class Krishnamurthy
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int n,p,rev,f,i,s=0;
        System.out.println("Enter Number");
        n= in.nextInt();
        p=n;
        while(n>0)
        {
            rev=n%10;
            f=1;
            for(i=rev;i>=1;i--)
            {
                f=f*i;//calculating factorial
            }
            s=s+f;//storing sum of factorial
            n=n/10;
        }
        if(p==s)
        {
            System.out.println("It is a Krishnamurthy Number : "+p);
        }
        else
        {
            System.out.println("It is not a Krishnamurthy Number : "+p);
        }
    }//end of main method
}// end of class


For Proper Understanding Watch the Video :-



Watch this video : Krishnamurthy Number in Java.

All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Composite Number in JAVA

Frequency of each digit of a number in Java

Fibonacci Prime Series in JAVA