TechoSagar: Krishnamurthy number

Krishnamurthy number

A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. Given a number N as input. Write a program to check whether this number is krishnamurthy number or not.

Input:
First line of input contains a single integer T which denotes the number of test cases. Then T test cases follows. First line of each test case contains a single integer N.

Output:
For each test case print "YES" without quotes if N is a Krishnamurthy Number otherwise print "NO".

Constraints:
1<=T<=100
1<=N<=105

Example:
Input:

2
145
235
Output:
YES
NO

Your Code
#include<iostream>

using namespace std;
int fact(int x)
{
int i;
int product=1;
for(i=1; i<=x; i++)
{
product=product*i;
}
return product;
}

int main()
{
int t ;
cin>>t;
while(t--)
{
int n;
cin>>n;
int y=n;
int sum=0;
while(n!=0)
{
int digit=n%10;
n=n/10;
sum=sum+fact(digit);
}
if(sum==y)
{
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;

}

}

Follow us

Follow Bijendra Kumar on Facebook