Program to Check the Number is a Krishnamurthy Number using For loop

Program to Check the Number is a Krishnamurthy Number using For loop

  • Write a program to Check the Number is a Krishnamurthy Number using For loop in C
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in C++
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in Python
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in PHP
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in Java
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in Java Script
  • Write a program to Check the Number is a Krishnamurthy Number using For loop in C#

Explanation:

A number that is equal to the sum of the factorials of its digits is known as a Krishnamurthy Number, or Strong Number. As an illustration, 145 is a Krishnamurthy number since 1! + 4! + 5! = 145
1! + 4! + 5! = 145.

Steps:

  1. The factorial function computes the factorial of a given digit.
  2. The number is converted to a string to allow iteration over each digit.
  3. For each digit, its factorial is calculated and added to a running total.
  4. Finally, the total sum of the factorials is compared to the original number. If they match, the number is a Krishnamurthy Number.

Program to Check the Number is a Krishnamurthy Number using For loop

#include <stdio.h>

int factorial(int num) {
    int fact = 1;
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

int is_krishnamurthy(int num) {
    int original = num, sum = 0;

    while (num > 0) {
        int digit = num % 10;
        sum += factorial(digit);
        num /= 10;
    }

    return sum == original;
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    if (is_krishnamurthy(n)) {
        printf("%d is a Krishnamurthy Number.\n", n);
    } else {
        printf("%d is not a Krishnamurthy Number.\n", n);
    }

    return 0;
}

#include <iostream>
using namespace std;

int factorial(int num) {
    int fact = 1;
    for (int i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

bool is_krishnamurthy(int num) {
    int original = num, sum = 0;

    while (num > 0) {
        int digit = num % 10;
        sum += factorial(digit);
        num /= 10;
    }

    return sum == original;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;

    if (is_krishnamurthy(n)) {
        cout << n << " is a Krishnamurthy Number." << endl;
    } else {
        cout << n << " is not a Krishnamurthy Number." << endl;
    }

    return 0;
}

def factorial(num):
    fact = 1
    for i in range(1, num + 1):
        fact *= i
    return fact

def is_krishnamurthy(num):
    original = num
    sum_of_factorials = 0

    while num > 0:
        digit = num % 10
        sum_of_factorials += factorial(digit)
        num //= 10

    return sum_of_factorials == original

n = int(input("Enter a number: "))

if is_krishnamurthy(n):
    print(f"{n} is a Krishnamurthy Number.")
else:
    print(f"{n} is not a Krishnamurthy Number.")

<?php
function factorial($num) {
    $fact = 1;
    for ($i = 1; $i <= $num; $i++) {
        $fact *= $i;
    }
    return $fact;
}

function is_krishnamurthy($num) {
    $original = $num;
    $sum = 0;

    while ($num > 0) {
        $digit = $num % 10;
        $sum += factorial($digit);
        $num = (int)($num / 10);
    }

    return $sum == $original;
}

$n = (int)readline("Enter a number: ");

if (is_krishnamurthy($n)) {
    echo "$n is a Krishnamurthy Number.\n";
} else {
    echo "$n is not a Krishnamurthy Number.\n";
}
?>

import java.util.Scanner;

public class KrishnamurthyNumber {

    public static int factorial(int num) {
        int fact = 1;
        for (int i = 1; i <= num; i++) {
            fact *= i;
        }
        return fact;
    }

    public static boolean isKrishnamurthy(int num) {
        int original = num, sum = 0;

        while (num > 0) {
            int digit = num % 10;
            sum += factorial(digit);
            num /= 10;
        }

        return sum == original;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = scanner.nextInt();

        if (isKrishnamurthy(n)) {
            System.out.println(n + " is a Krishnamurthy Number.");
        } else {
            System.out.println(n + " is not a Krishnamurthy Number.");
        }

        scanner.close();
    }
}

<div class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950"><div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-javascript">function factorial(num) {
    let fact = 1;
    for (let i = 1; i <= num; i++) {
        fact *= i;
    }
    return fact;
}

function isKrishnamurthy(num) {
    let original = num;
    let sum = 0;

    while (num > 0) {
        let digit = num % 10;
        sum += factorial(digit);
        num = Math.floor(num / 10);
    }

    return sum === original;
}

let n = parseInt(prompt("Enter a number: "));

if (isKrishnamurthy(n)) {
    console.log(`${n} is a Krishnamurthy Number.`);
} else {
    console.log(`${n} is not a Krishnamurthy Number.`);
}

using System;

class Program {
    static int Factorial(int num) {
        int fact = 1;
        for (int i = 1; i <= num; i++) {
            fact *= i;
        }
        return fact;
    }

    static bool IsKrishnamurthy(int num) {
        int original = num, sum = 0;

        while (num > 0) {
            int digit = num % 10;
            sum += Factorial(digit);
            num /= 10;
        }

        return sum == original;
    }

    static void Main() {
        Console.Write("Enter a number: ");
        int n = int.Parse(Console.ReadLine());

        if (IsKrishnamurthy(n)) {
            Console.WriteLine($"{n} is a Krishnamurthy Number.");
        } else {
            Console.WriteLine($"{n} is not a Krishnamurthy Number.");
        }
    }
}

List of All Programs