Posts: 6,843
Threads: 4
Joined: Mar 2015
Question 44 : Write a program in Java to check whether the number given by the user is a Krishna Murthy number or not.
Krishna Murthy Number : It is a number which is equal to the sum of the factorials of all its digits.
For example : 145 = 1! + 4! + 5! = 1 + 24 + 120 = 145
Java Program :
import java.io.*;
class krishnamurthy
{
int fact(int n)
{
int i,p=1;
for(i=n;i>=1;i--)
p=p*i;
return p;
}
void krishna()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b,s=0;
System.out.print("Enter the number : ");
a=Integer.parseInt(br.readLine());
System.out.print("\n");
int n=a;
while(a>0)
{
b=a%10;
s=s+fact(b);
a=a/10;
}
if(s==n)
System.out.print(n+" is a krishnamurthy number");
else
System.out.print(n+" is not a krishnamurthy number");
}
}