JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Strong Number
Photo Credit to CodeToFun
π Introduction
In the world of programming, exploring number properties is a fascinating journey. One such property is related to Strong Numbers.
A Strong Number (also known as a Digital Factorial) is a number in which the sum of the factorials of its digits is equal to the number itself.
In this tutorial, we'll dive into a JavaScript program that checks whether a given number is a Strong Number or not.
π Example
Let's take a look at the JavaScript code that checks whether a given number is a Strong Number.
// Function to calculate the factorial of a number
function factorial(num) {
if (num === 0 || num === 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}
// Function to check if a number is a Strong Number
function isStrongNumber(num) {
const originalNum = num;
let sum = 0;
while (num > 0) {
const digit = num % 10;
sum += factorial(digit);
num = Math.floor(num / 10);
}
return sum === originalNum;
}
// Replace this value with your desired number
const number = 145;
// Check if the number is a Strong Number
if (isStrongNumber(number)) {
console.log(`${number} is a Strong Number.`);
} else {
console.log(`${number} is not a Strong Number.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
145 is a Strong Number.
Run the script to check if the number is a Strong Number.
π§ How the Program Works
- The program defines a function factorial to calculate the factorial of a number.
- Another function isStrongNumber is defined to check if a given number is a Strong Number.
- Another function isStrongNumber is defined to check if a given number is a Strong Number.
- The result is compared with the original number to determine if it is a Strong Number.
π§ Understanding the Concept of Strong Number
To determine if a number is a Strong Number, we need to calculate the factorial of each digit of the number and then sum these factorials. If the sum is equal to the original number, then it is a Strong Number.
For example, let's consider the number 145:
- The factorial of 1 is 1.
- The factorial of 4 is 24.
- The factorial of 5 is 120.
The sum of these factorials is 1 + 24 + 120 = 145, which is the original number. Hence, 145 is a Strong Number.
π Conclusion
Understanding and implementing programs that check for mathematical properties, such as Strong Numbers, enhances problem-solving skills in programming. Feel free to modify and expand upon this code for your specific needs. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Strong Number), please comment here. I will help you immediately.