Check if a number is a Krishnamurthy Number or not - GeeksforGeeks

07 Apr, 2021
A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself. For example 145, sum of factorial of each digits: 1! + 4! + 5! = 1 + 24 + 120 = 145
Examples: 
Input : 145
Output : YES
Explanation: 1! + 4! + 5! = 
1 + 24 + 120 = 145, which is equal to input,
hence YES.

Input : 235
Output : NO
Explanation: 2! + 3! + 5! = 
2 + 6 + 120 = 128, which is not equal to input, 
hence NO.
The idea is simple, we compute sum of factorials of all digits and then compare the sum with n. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check if a number// is a krishnamurthy number#include <bits/stdc++.h>using namespace std; // Function to calculate the factorial of any numberint factorial(int n){    int fact = 1;    while (n != 0) {        fact = fact * n;        n--;    }    return fact;} // function to Check if number is krishnamurthybool 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    return (sum == n);} // Driver codeint main(){    int n = 145;    if (isKrishnamurthy(n))        cout << "YES";    else        cout << "NO";    return 0;}
// Java program to check if a number// is a krishnamurthy number.import java.util.*;import java.io.*; class Krishnamurthy {    // function to calculate the factorial    // of any number    static int factorial(int n)    {        int fact = 1;        while (n != 0) {            fact = fact * n;            n--;        }        return fact;    }     // function to Check if number is krishnamurthy    static boolean 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        return (sum == n);    }     // Driver code    public static void main(String[] args)    {        int n = 145;        if (isKrishnamurthy(n))            System.out.println("YES");        else            System.out.println("NO");    }}
# Python program to check if a number# is a krishnamurthy number # function to calculate the factorial# of any numberdef factorial(n) :    fact = 1    while (n != 0) :        fact = fact * n        n = n - 1    return fact # function to Check if number is# krishnamurthy/specialdef isKrishnamurthy(n) :    sum = 0    temp = n    while (temp != 0) :         # calculate factorial of last digit        # of temp and add it to sum        rem = temp%10        sum = sum + factorial(rem)         # replace value of temp by temp / 10        temp = temp // 10             # Check if number is krishnamurthy    return (sum == n) # Driver coden = 145if (isKrishnamurthy(n)) :    print("YES")else :    print("NO")  # This code is contributed by Prashant Aggarwal
// C# program to check if a number// is a krishnamurthy number.using System; class GFG {         // function to calculate the    // factorial of any number    static int factorial(int n)    {        int fact = 1;                 while (n != 0) {            fact = fact * n;            n--;        }                 return fact;    }     // function to Check if number is    // krishnamurthy    static bool 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        return (sum == n);    }     // Driver code    public static void Main()    {        int n = 145;        if (isKrishnamurthy(n))            Console.Write("YES");        else            Console.Write("NO");    }} // This code is contributed by nitin mittal.
<?php// PHP program to check if a number// is a krishnamurthy number // Function to find Factorial// of any numberfunction factorial($n){    $fact = 1;    while($n != 0)    {        $fact = $fact * $n;        $n--;    }         return $fact;} // function to Check if number// is krishnamurthyfunction isKrishnamurthyNumber($n){    $sum = 0;         // Storing the value in    // other variable    $temp = $n;         while($temp != 0)    {        // calculate factorial of last digit        // of temp and add it to sum        $sum = $sum + factorial($temp % 10);                 // Integer Division        // replace value of temp by temp/10        $temp = intdiv($temp, 10);    }         // Check if number is krishnamurthy    return $sum == $n;} // Driver code$n = 145; if (isKrishnamurthyNumber($n))    echo "YES";else    echo "NO";  // This code is contributed by akash7981?>
<script>     // Javascript program to check if a number// is a krishnamurthy number // Function to find Factorial// of any numberfunction factorial(n){    let fact = 1;         while (n != 0)    {        fact = fact * n;        n--;    }         return fact;} // Function to check if number// is krishnamurthyfunction isKrishnamurthyNumber(n){    let sum = 0;         // Storing the value in    // other variable    let temp = n;         while (temp != 0)    {                 // Calculate factorial of last digit        // of temp and add it to sum        sum = sum + factorial(temp % 10);                 // Integer Division        // replace value of temp by temp/10        temp = parseInt(temp / 10);    }         // Check if number is krishnamurthy    return sum == n;} // Driver codelet n = 145; if (isKrishnamurthyNumber(n))    document.write("YES");else    document.write("NO"); // This code is contributed by _saurabh_jaiswal </script>
Output: 
YES
Interestingly, there are exactly four Krishnamurthy numbers i.e. 1, 2, 145, and 40585 known to us.  
YouTubeGeeksforGeeks501K subscribersKrishnamurthy Numbers | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 5:55•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=U5jJkZYG0dQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
prshntaggrwl
_saurabh_jaiswal
factorial
Mathematical
School Programming
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Modular multiplicative inverse
Program to multiply two matrices
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java