1.
Introduction
2.
The Logic of Armstrong Number in JavaScript
3.
Examples of Armstrong Number in JavaScript
3.1.
Example 1: Function to Check Armstrong Number
3.2.
JavaScript
3.3.
Example 2: Finding Armstrong Numbers in a Range
3.4.
JavaScript
4.
Check Armstrong Number of n Digits
4.1.
Dynamic Armstrong Number Checker
4.2.
JavaScript
5.
5.1.
Why are Armstrong numbers significant in computer science?
5.2.
Can Armstrong numbers have any practical applications?
5.3.
How does understanding Armstrong numbers benefit JavaScript learners?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

# Armstrong Number in Javascript

Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

## Introduction

Armstrong numbers, a fascinating concept in mathematics, hold a special intrigue in the realm of coding, especially for students like you delving into JavaScript. These numbers are unique because the sum of their own digits, each raised to the power of the number of digits, equals the number itself. For instance, 153 is an Armstrong number as 13+53 +33=153.

Understanding and implementing the logic to identify these numbers in JavaScript not only sharpens your programming skills but also enhances your problem-solving abilities.

## The Logic of Armstrong Number in JavaScript

To identify an Armstrong number in JavaScript, we follow a specific algorithm. Let's break it down step-by-step:

• Get the Number of Digits: First, determine the number of digits in the given number. This can be done by converting the number into a string and then getting its length.

• Split the Number into Digits: Convert the number into an array of its digits.

• Calculate the Sum: Raise each digit to the power of the total number of digits and sum them up.

• Compare with the Original Number: If the sum equals the original number, it's an Armstrong number.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Examples of Armstrong Number in JavaScript

• JavaScript

### JavaScript

``function isArmstrongNumber(number) {    let sum = 0;    const numberOfDigits = number.toString().length;    let temp = number;    while (temp > 0) {        let digit = temp % 10;        sum += Math.pow(digit, numberOfDigits);        temp = Math.floor(temp / 10);    }    return sum === number;}console.log(isArmstrongNumber(153)); console.log(isArmstrongNumber(123)); ``

Output

In this example, isArmstrongNumber function does the following:

It calculates the number of digits in number.

Iterates over each digit, raises it to the power of numberOfDigits, and adds it to sum.

Finally, it checks if sum is equal to the original number.

### Example 2: Finding Armstrong Numbers in a Range

Let's take a step further & write a function that finds all Armstrong numbers within a given range.

• JavaScript

### JavaScript

``function isArmstrongNumber(number) {    let sum = 0;    const numberOfDigits = number.toString().length;    let temp = number;    while (temp > 0) {        let digit = temp % 10;        sum += Math.pow(digit, numberOfDigits);        temp = Math.floor(temp / 10);    }    return sum === number;}function findArmstrongNumbers(start, end) {    let armstrongNumbers = [];    for (let i = start; i <= end; i++) {        if(isArmstrongNumber(i)) {            armstrongNumbers.push(i);        }    }    return armstrongNumbers;}console.log(findArmstrongNumbers(100, 999));``

Output

This function findArmstrongNumbers uses the previously defined isArmstrongNumber to check each number in the specified range.

## Check Armstrong Number of n Digits

Expanding our exploration, let's write a JavaScript function that can check if a number is an Armstrong number for any number of digits. This function will be versatile, working for any n-digit number.

• JavaScript

### JavaScript

``function isNDigitArmstrongNumber(number) {    let sum = 0;    const numberOfDigits = number.toString().length;    let temp = number;    while (temp > 0) {        let digit = temp % 10;        sum += Math.pow(digit, numberOfDigits);        temp = Math.floor(temp / 10);    }    return sum === number;}console.log(isNDigitArmstrongNumber(9474)); console.log(isNDigitArmstrongNumber(1634)); console.log(isNDigitArmstrongNumber(8208)); ``

Output

In this enhanced function, isNDigitArmstrongNumber, the core logic remains the same as our initial function. However, it's now capable of checking Armstrong numbers with any number of digits, making it more flexible and useful.

### Why are Armstrong numbers significant in computer science?

Armstrong numbers are more than just a mathematical curiosity; they're used in computer science for educational purposes to teach algorithms, loops, and conditional logic. They are excellent for honing problem-solving and coding skills, especially for students learning a new programming language like JavaScript.

### Can Armstrong numbers have any practical applications?

While Armstrong numbers are primarily used for educational and testing purposes, their unique properties can be leveraged in cryptographic applications and pattern recognition tasks. However, their real-world applications are somewhat limited compared to their educational value.

### How does understanding Armstrong numbers benefit JavaScript learners?

For JavaScript learners, working with Armstrong numbers provides a hands-on experience in dealing with number manipulation, loops, and conditional statements. This enhances their understanding of JavaScript's syntax and logic, paving the way for tackling more complex coding challenges.

## Conclusion

In this exploration of Armstrong numbers in JavaScript, we've delved deep into their definition, significance, and the logic to identify them. We've provided practical code examples for checking Armstrong numbers for any number of digits and within a range. This topic not only enriches your understanding of JavaScript but also sharpens your problem-solving skills. Whether you're a beginner or an advanced learner, grappling with such concepts solidifies your coding foundation and prepares you for more challenging programming tasks ahead.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc.

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass