How to Implement Armstrong Number in JavaScript?
In this, we will understand how to implement a JavaScript program to check if a number is an Armstrong number. We'll explain both the algorithm and provide the corresponding code.
Algorithm of Armstrong Number Program in JavaScript
Follow these steps to implement the Armstrong number program in JavaScript:
- Declare a Function: Define a function named checkArmstrongNumber that takes no arguments.
- Initialize Variables:
num: Store the input number.
originalNum: Preserve the original value of the number.
cubeSum: Initialize the sum of the cubes of digits to 0.
digit: Store the current digit extracted from num.
- Read Input: Use prompt() to read the number from the user and assign it to num.
- Extract Digits and Compute Cube:
Use a while loop to iterate until num becomes 0.
Inside the loop, extract the last digit using num % 10 and store it in digit.
Compute the cube of the digit using Math.pow(digit, 3) and add it to cubeSum.
- Update num: Divide num by 10 and update it using Math.floor(num / 10) to truncate the decimal part.
- Compare the Sum: After the loop ends, compare the cubeSum with originalNum:
If they are equal, print that the number is an Armstrong number.
Otherwise, print that the number is not an Armstrong number.
JavaScript Code Example
function checkArmstrongNumber() {
let num = parseInt(prompt("Enter a number: "));
let originalNum = num;
let cubeSum = 0;
let digit;
while (num !== 0) {
digit = num % 10;
cubeSum += Math.pow(digit, 3);
num = Math.floor(num / 10);
}
if (cubeSum === originalNum) {
console.log(`${originalNum} is an Armstrong number.`);
} else {
console.log(`${originalNum} is not an Armstrong number.`);
}
}
checkArmstrongNumber();

You can also try this code with Online Javascript Compiler
Run Code
Output
Enter a number: 374
374 is not an Armstrong number.
Examples of Armstrong Number in JavaScript
Example 1: Function to Check Armstrong Number
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));

You can also try this code with Online Javascript Compiler
Run Code
Output
true
false
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
Here’s how to find all Armstrong numbers within a given range in 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
[153, 370, 371, 407]
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.
Dynamic Armstrong Number Checker
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
true
true
true
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.
Frequently Asked Questions
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.