Print Strong Numbers Within a Range using JavaScript Last Updated : 14 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In mathematics, a strong number is a number whose sum of the factorial of its digits is equal to the number itself. The task is to print all strong numbers within a given range in JavaScript. Example: 145 is a Strong number because: 1: 14: 4*3*2*1=245: 5*4*3*2*1=120120+24+1=145Since the sum is equal to number, 145 is a Strong number. Table of Content Iterative Approach:Recursive Approach:Iterative ApproachIn this approach, we iterate through the range of numbers and for each number, we calculate the sum of the factorials of its digits iteratively. Then, we check if this sum is equal to the original number. Example: The below code print Strong Numbers Within a Range using Iterative Approach in JavaScript. JavaScript function factorial(num) { let fact = 1; for (let i = 2; i <= num; i++) { fact *= i; } return fact; } function sumOfFactorialOfDigits(number) { let sum = 0; let temp = number; while (temp > 0) { let digit = temp % 10; sum += factorial(digit); temp = Math.floor(temp / 10); } return sum; } function printStrongNumbersInRange(start, end) { for (let i = start; i <= end; i++) { if (sumOfFactorialOfDigits(i) === i) { console.log(i); } } } printStrongNumbersInRange(1, 1000); Output1 2 145 Recursive ApproachIn this approach, we define a recursive function to calculate the sum of the factorials of the digits of a number. Then, we iterate through the range of numbers and for each number, we call this recursive function to check if it is a strong number. Example: The below code print Strong Numbers Within a Range using Recursive Approach in JavaScript. JavaScript function factorial(num) { if (num === 0 || num === 1) { return 1; } else { return num * factorial(num - 1); } } function sumOfFactorialOfDigits(number) { if (number === 0) { return 0; } else { let digit = number % 10; return factorial(digit) + sumOfFactorialOfDigits(Math.floor(number / 10)); } } function isStrongNumber(number) { return sumOfFactorialOfDigits(number) === number; } function printStrongNumbersInRange(start, end) { for (let i = start; i <= end; i++) { if (isStrongNumber(i)) { console.log(i); } } } printStrongNumbersInRange(1, 1000); Output1 2 145 Comment More infoAdvertise with us Next Article How to print a number with commas as thousands separators in JavaScript? A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to print a web page using JavaScript ? Javascript is the world most popular lightweight, cross-platform, interpreted compiled programming language, along with a scripting language for web. It facilitates the dynamic functionality that helps to make the interactive website. As all the Morden browsers use the Javascript & is a client-s 2 min read Generate Random Number in Given Range Using JavaScript Here are the different ways to generate random numbers in a given range using JavaScript1. Using Math.random(): basic ApproachThis is the simplest way to generate a random number within a range using Math.random().JavaScriptlet min = 10; let max = 20; let random = Math.floor(Math.random() * (max - m 3 min read How to print a number with commas as thousands separators in JavaScript? Method 1: Using Intl.NumberFormat() The Intl.NumberFormat() object is used to represent numbers in language-sensitive formatting. It can be used to represent currency or percentages according to the locale specified.The locales parameter of this object is used to specify the format of the number. Th 2 min read Extract a Number from a String using JavaScript We will extract the numbers if they exist in a given string. We will have a string and we need to print the numbers that are present in the given string in the console.Below are the methods to extract a number from string using JavaScript:Table of ContentUsing JavaScript match method with regExUsing 4 min read Print all Odd Numbers in a Range in JavaScript Array Odd numbers are numbers that cannot be divided into equal parts. If we divide the odd number by 2, the remainder is 1. In JavaScript, if we want to print all Odd numbers in a range, we can print it by iterating over the array, applying the condition, and printing the odd numbers. There are various a 4 min read JavaScript Number Static Properties The JavaScript Number is an object that can represent numeric values like integer, float, hexadecimal, decimal, etc. It acts as a wrapper object for primitive numeric values. It has various methods and properties that are used to perform different tasks on numbers. The following table shows some sta 2 min read Like