Check Whether a Number is Strong Number in JavaScript
Last Updated :
13 Mar, 2024
Strong Numbers are the numbers whose sum of the factorial of digits is equal to the original number. In JavaScript, we can check whether the number is a strong number or not using various approaches. In this article, we will explore all the possible approaches with their practical implementation.
These are the following methods:
Using Array reduce() method
In this approach, we are using the reduce() method to iteratively calculate the sum of factorials for each digit of the given number by converting it to an array of digits, applying the factorial function (fact), and accumulating the results. The final comparison determines whether the sum equals the original number, which prints the output in a 'Yes' or 'No' format.
Syntax:
array.reduce(callback, initialValue);
Example: The below example uses the reduce() function to Check Whether a Number is a Strong Number in JavaScript.
JavaScript
let fact = (num) => (num <= 1 ? 1 :
num * fact(num - 1));
let res = (n) => {
let sum = n
.toString()
.split('')
.reduce((acc, digit) => acc +
fact(Number(digit)), 0);
return sum === n ? 'Yes' : 'No';
};
console.log(res(145));
console.log(res(534));
Using for...of Loop
In this approach, we are using the for...of loop, the fact() function calculates the factorial of a given number. The res() function then iterates over each digit of the input number (n) using for...of, converts them to numbers, applies the factorial function, and stores the results in the sum variable.
Syntax:
for (variable of iterable) {
// code
}
Example: The below example uses for...of Loop to Check Whether a Number is a Strong Number in JavaScript.
JavaScript
let fact = (num) => {
let result = 1;
for (let i = 2; i <= num; i++) {
result *= i;
}
return result;
};
let res = (n) => {
let sum = 0;
for (let digit of String(n)
.split('').map(Number)) {
sum += fact(digit);
}
return sum === n ? 'Yes' : 'No';
};
console.log(res(145));
console.log(res(534));
Using while Loop
In this approach, we are using a while loop to iteratively process each digit of the input number (n). The loop extracts the last digit (digit) in each iteration, calculates its factorial using a nested for loop, and stores the factorials in the sum variable. The num variable is updated by removing the last digit in each iteration. Then the final comparison checks whether the sum is equal to the original input number and prints Yes or No as output.
Syntax:
while (condition) {
// code
}
Example: The below example uses while Loop to Check Whether a Number is a Strong Number in JavaScript.
JavaScript
let res = (n) => {
let sum = 0;
let num = n;
while (num > 0) {
let digit = num % 10;
let fact = 1;
for (let i = 2; i <= digit; i++) {
fact *= i;
}
sum += fact;
num = Math.floor(num / 10);
}
return sum === n ? 'Yes' : 'No';
};
console.log(res(145));
console.log(res(534));
Similar Reads
JavaScript Program to Check whether the Input Number is a Neon Number A neon number is a number where the sum of digits of its square equals the number itself. For example, 9 is a neon number because 9^2 = 81, and the sum of the digits 81 is 9. There are several ways to check if a given number is a neon number in JavaScript or not which are as follows: Table of Conten
2 min read
How to check whether a number is NaN or finite in JavaScript ? When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN
3 min read
How to Check if a Value is a Number in JavaScript ? To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.Methods to Check if a Value is a NumberThere are various ways to check if a value
3 min read
JavaScript Program to Check if a Number is Odd or Even We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.Even = {2k : k â Z}(whe
3 min read
JavaScript Program to Check if a Number is Float or Integer In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read