CppBuzz.com
  
Home C C++ Java Python Perl PHP SQL JavaScript Linux Online Test Forum

Home » C++ » C++ program on Krishnamurthy number

C++ Program to check if number is Krishnamurthy

This C++ program takes input from user and check if it is Krishnamurthy. Program is divided into functions factorial(int) and isKrishnamurthy(int). For more details please run this code.

#include<iostream>
using namespace std;
 
//Function to calculate the factorial of any number
int factorial(int n)
{
int fact = 1;
while (n != 0)
{
   fact = fact*n;
   n--;
}
return fact;
}
 
//function to Check if number is krishnamurthy
void isKrishnamurthy(int n)
{
int sum = 0;

int temp = n;
while (temp != 0)
{
// calculate factorial of last digit
// of temp and add it to sum

sum += factorial(temp%10);

// replace value of temp by temp/10
temp = temp/10;
}

// Check if number is krishnamurthy

if(sum == n)
cout << "Yes. Input no is a Krishnamurthy";
else
cout << "No. Input no is not a Krishnamurthy";
}
 
int main()
{
int n;
cout<<"\n Input any no : ";
cin>>n;

isKrishnamurthy(n);

return 0;
}