Krishnamurthy Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>

int fact(int n) {
    if(n==0 || n==1) return 1;
    return n * fact(n-1);
}

int main() {
    int num, temp, sum=0;
    scanf("%d", &num);
    temp = num;
    while(temp > 0) {
        sum += fact(temp % 10);
        temp /= 10;
    }
    if(sum == num) printf("Krishnamurthy Number");
    else printf("Not a Krishnamurthy Number");
    return 0;
}

C Output

Input:
145
Output:
Krishnamurthy Number


C++ Program

#include <iostream>
using namespace std;

int fact(int n) {
    if(n==0 || n==1) return 1;
    return n * fact(n-1);
}

int main() {
    int num, temp, sum=0;
    cin >> num;
    temp = num;
    while(temp > 0) {
        sum += fact(temp % 10);
        temp /= 10;
    }
    if(sum == num) cout << "Krishnamurthy Number";
    else cout << "Not a Krishnamurthy Number";
}

C++ Output

Input:
125
Output:
Not a Krishnamurthy Number


JAVA Program

import java.util.Scanner;

public class Main {
    static int fact(int n) {
        if(n==0 || n==1) return 1;
        return n * fact(n-1);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt(), temp=num, sum=0;
        while(temp > 0) {
            sum += fact(temp % 10);
            temp /= 10;
        }
        if(sum == num) System.out.println("Krishnamurthy Number");
        else System.out.println("Not a Krishnamurthy Number");
    }
}

JAVA Output

Input:
40585
Output:
Krishnamurthy Number


Python Program

def fact(n):
    return 1 if n==0 or n==1 else n*fact(n-1)

num = int(input())
temp, s = num, 0
while temp > 0:
    s += fact(temp % 10)
    temp //= 10

print("Krishnamurthy Number" if s==num else "Not a Krishnamurthy Number")

Python Output

Input:
78
Output:
Not a Krishnamurthy Number


In-Depth Learning – Entire Concept in Paragraphs
Example
A Krishnamurthy Number is a number which is the same as the sum of the factorials of all its digits. For instance, 145 is one because:
1! + 4! + 5! = 1 + 24 + 120 = 145. 40585 is also such a number. But most numbers aren't Krishnamurthy numbers because factorials increase very quickly, so it is an unusual characteristic.

Real-Life Analogy
Imagine every digit of a number as a "worker" who is working on a project. The factorial of a digit represents the overall effort of that worker, taking into account all arrangements of tasks. For the project (the number) to be accomplished, the overall effort of all the workers (factorials) should exactly match the project target (the number itself). If it is so, the project is perfectly in balance — similar to a Krishnamurthy Number.

Why It Matters
The topic introduces factorials and their usage. Factorials play a significant role in mathematics, particularly in permutations, combinations, and probability. By relating factorials to digits, the Krishnamurthy Number problem forces students to exercise recursion or loops, modulus operations, and digit extraction — three of the most essential programming building blocks.

Learning Insights
This problem educates recursion via factorial, digit manipulation with % and /, and sum-based conditions. It also demonstrates how mathematical principles such as factorials can be merged with programming to verify unusual numerical attributes. These kinds of problems enhance algorithmic thinking and make you realize how to break down a number into smaller pieces.

Interview and Real-World Relevance
Even though Krishnamurthy numbers themselves are not common, the question tends to be used in coding interviews to verify recursion, loops, and mathematical knowledge. It also serves to condition number theory logic, which comes in handy with areas such as cryptography and computer security, where distinct characteristics of numbers are significant. From a project point of view, these principles also apply to data validation, algorithm formulation, and creating unique identifiers.

Final SEO-Friendly Insight
Realizing the Krishnamurthy Number is a great practice for programming beginners. It involves recursion, digit extraction, and mathematical reasoning all within one problem. Learning to determine if a number is a Krishnamurthy number helps students master working with numbers, calling recursive functions efficiently, and reinforcing problem-solving strategies that are most often asked in programming interviews and competitive programming contests. This subject is trending among students in their quest to solve number-based questions in C, C++, Java, and Python and hence practical and relevant to scholarly and professional development.