Program to Check the Number is a Krishnamurthy Number using While loop
- Write a program to Check the Number is a Krishnamurthy Number using While loop in C
- Write a program to Check the Number is a Krishnamurthy Number using While loop in C++
- Write a program to Check the Number is a Krishnamurthy Number using While loop in Python
- Write a program to Check the Number is a Krishnamurthy Number using While loop in PHP
- Write a program to Check the Number is a Krishnamurthy Number using While loop in Java
- Write a program to Check the Number is a Krishnamurthy Number using While loop in Java Script
- Write a program to Check the Number is a Krishnamurthy Number using While 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:
- The factorial function computes the factorial of a given digit.
- The number is converted to a string to allow iteration over each digit.
- For each digit, its factorial is calculated and added to a running total.
- 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 While loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
<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-c">#include <stdio.h>
// Function to calculate the factorial of a number
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to check if the number is a Krishnamurthy number
int isKrishnamurthy(int num) {
int temp = num, sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += factorial(digit); // Add factorial of each digit
temp /= 10;
}
return sum == num;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isKrishnamurthy(num)) {
printf("%d is a Krishnamurthy number.\n", num);
} else {
printf("%d is not a Krishnamurthy number.\n", num);
}
return 0;
}
#include <iostream>
using namespace std;
// Function to calculate factorial of a number
int factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
// Function to check if the number is Krishnamurthy
bool isKrishnamurthy(int num) {
int temp = num, sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += factorial(digit); // Add factorial of each digit
temp /= 10;
}
return sum == num;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isKrishnamurthy(num)) {
cout << num << " is a Krishnamurthy number." << endl;
} else {
cout << num << " 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):
temp = num
sum = 0
while temp != 0:
digit = temp % 10
sum += factorial(digit)
temp //= 10
return sum == num
num = int(input("Enter a number: "))
if is_krishnamurthy(num):
print(f"{num} is a Krishnamurthy number.")
else:
print(f"{num} is not a Krishnamurthy number.")
<?php
function factorial($num) {
$fact = 1;
for ($i = 1; $i <= $num; $i++) {
$fact *= $i;
}
return $fact;
}
function isKrishnamurthy($num) {
$temp = $num;
$sum = 0;
while ($temp != 0) {
$digit = $temp % 10;
$sum += factorial($digit); // Add factorial of each digit
$temp = (int)($temp / 10);
}
return $sum == $num;
}
$num = (int)readline("Enter a number: ");
if (isKrishnamurthy($num)) {
echo "$num is a Krishnamurthy number.\n";
} else {
echo "$num is not a Krishnamurthy number.\n";
}
?>
import java.util.Scanner;
public class Krishnamurthy {
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 temp = num, sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += factorial(digit); // Add factorial of each digit
temp /= 10;
}
return sum == num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (isKrishnamurthy(num)) {
System.out.println(num + " is a Krishnamurthy number.");
} else {
System.out.println(num + " is not a Krishnamurthy number.");
}
}
}
function factorial(num) {
let fact = 1;
for (let i = 1; i <= num; i++) {
fact *= i;
}
return fact;
}
function isKrishnamurthy(num) {
let temp = num;
let sum = 0;
while (temp !== 0) {
let digit = temp % 10;
sum += factorial(digit); // Add factorial of each digit
temp = Math.floor(temp / 10);
}
return sum === num;
}
let num = parseInt(prompt("Enter a number: "));
if (isKrishnamurthy(num)) {
console.log(`${num} is a Krishnamurthy number.`);
} else {
console.log(`${num} 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 temp = num, sum = 0;
while (temp != 0) {
int digit = temp % 10;
sum += Factorial(digit); // Add factorial of each digit
temp /= 10;
}
return sum == num;
}
static void Main() {
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
if (IsKrishnamurthy(num)) {
Console.WriteLine($"{num} is a Krishnamurthy number.");
} else {
Console.WriteLine($"{num} is not a Krishnamurthy number.");
}
}
}