Question

Check whether a number is Krishnamurthy number(also called Strong number) using recursion.

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

				
					eg,

145=1!+4!+5!

=1+24+120

=145

Hence, 145 IS KRISHNAMURTHY NUMBER
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class KrishnamurthyNumber
{
    long num=0,temp=0;
    public void accept()
    {

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        num=sc.nextLong();

    }

    public static long Factorial(long n)
    {
        if(n==1)
        {
            return 1;
        }
        else
        {
            return n*Factorial(n-1);
        }
    }

    public void checkKrishnamurthy()
    {
        long sum=0;
        temp=num;
        while(temp>0)
        {
            sum=sum+Factorial(temp%10);
            temp/=10;
        }
        if(sum==num)
        {
            System.out.print(num+ " IS A KRISHNAMURTHY NUMBER");   
        }
        else
        {
            System.out.print(num+ " IS NOT A KRISHNAMURTHY NUMBER");   
        }
    }


    public static void main()
    {
        KrishnamurthyNumber ob1=new KrishnamurthyNumber();
        ob1.accept();
        ob1.checkKrishnamurthy();

    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *