Open In App

Check Whether a Number is Strong Number in JavaScript

Last Updated : 13 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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));

Output
Yes
No

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));

Output
Yes
No

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));

Output
Yes
No


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads