Question

Print Krishnamurthy number(also called Strong number) in given range 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
				
			
				
					ENTER THE LOWER NUMBER
1
ENTER THE UPPER NUMBER
40585
KRISNAMURTHY NUMBER(S) BETWEEN 1 AND 40585:
1 2 145 40585 
				
			

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 lowerRange=0,upperRange=0,temp=0,i=0;
    public void accept()
    {

        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE LOWER NUMBER");
        lowerRange=sc.nextLong();
        System.out.println("ENTER THE UPPER NUMBER");
        upperRange=sc.nextLong();

    }

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

    public void checkKrishnamurthy()
    {

        long sum=0;
        System.out.println("KRISNAMURTHY NUMBER(S) BETWEEN "+lowerRange+" AND "+upperRange+":");
        for(i=lowerRange;i<=upperRange;i++)
        {
            temp=i;
            sum=0;
            while(temp>0)
            {
                sum=sum+Factorial(temp%10);
                temp/=10;
            }
            if(sum==i)
            {
                System.out.print(i+ " ");   
            }

        }

    }

    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 *