.Js
Write a code to find a Krishnamurthy/Strong number:
A Krishnamurthy number (also known as a strong number or special number) is a number whose sum of the factorial of its digits is equal to the number itself. Here's a JavaScript function to check if a given number is a Krishnamurthy number:
js Copy Code
function isKrishnamurthy(number) {
// Function to calculate the factorial of a number
function factorial(n) {
if (n === 0 || n === 1)
return 1;
else
return n * factorial(n - 1);
}
let sum = 0;
let originalNumber = number;
// Calculate the sum of the factorial of digits
while (number > 0) {
let digit = number % 10;
sum += factorial(digit);
number = Math.floor(number / 10);
}
// Check if the sum equals the original number
return sum === originalNumber;
}
// Example usage
let num = 145;
if (isKrishnamurthy(num))
console.log(num + " is a Krishnamurthy number.");
else
console.log(num + " is not a Krishnamurthy number.");
Output:
145 is a Krishnamurthy number.
Explanation:
You can replace 'num' with any number you want to check for Krishnamurthy property. If the number is a Krishnamurthy number, it will print "is a Krishnamurthy number." Otherwise, it will print "is not a Krishnamurthy number."
• In-depth concepts of Krishnamurthy number from the algorithm page.
What's Next?
We've now entered the finance section on this platform, where you can enhance your financial literacy.