Hi, friends are you wondering for a bog to understand Armstrong number in JavaScript? In this article, we will know completely what is Armstrong number and how to get an Armstrong number in javascript.

What is Armstrong’s number in javascript?
An Armstrong number is a number that is equal to the sum of powers of every digit to a number of digits. for example 153
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
number of digits =3 (153) so the power will be 3
How to Check if a Number is an Armstrong Number in javascript
To check or verify whether a given number is an Armstrong number, you can follow these steps:
- Convert the number to a string to count the number of digits.
- define or initialize a variable to keep track of the sum.
- Iterate through each digit of the number:
- Convert the digit back to an integer if it’s in string format.
- Raise the digit to the power of the number of digits.
- Add the result to the sum.
- Check if the sum is equal to the original number.
This is the way you can check whether a number is an Armstrong number in javascript or any other language using the same logic.
Armstrong number in javascript program
SO for now we have seen what is Armstrong’s number and now we will see the program to check whether a number is Armstrong or not in Javacsript.
Program to check if a number is Armstrong in javascript using for loop
function isArmstrongNumber(number) {
const numStr = number.toString();
const numDigits = numStr.length;
let total = 0;
for (let i = 0; i < numDigits; i++) {
total += Math.pow(parseInt(numStr[i]), numDigits);
}
return total === number;
}
Program to find Armstrong numbers in the given range of numbers
here we will use above defined program to check Armstrong’s number isArmstrongNumber
function findArmstrongNumbersInRange(start, end) {
const armstrongNumbers = [];
for (let i = start; i <= end; i++) {
if (isArmstrongNumber(i)) {
armstrongNumbers.push(i);
}
}
return armstrongNumbers;
}
// example
const startRange = 1;
const endRange = 1000;
const armstrongNumbersInRange = findArmstrongNumbersInRange(startRange, endRange);
console.log(`Armstrong numbers in the range ${startRange} to ${endRange}:`, armstrongNumbersInRange);
Without using for loop
if you want to check Armstrong’s number without for loop then you can use the map function to loop through and add numbers to get Armstrong’s number in javascript
function isArmstrongNumber(number) {
const numStr = number.toString();
const numDigits = numStr.length;
const sum = numStr
.split('')
.map(digit => Math.pow(parseInt(digit), numDigits))
.reduce((acc, val) => acc + val, 0);
return sum === number;
}
// Test the function with some examples
console.log(isArmstrongNumber(153)); // Should return true
console.log(isArmstrongNumber(370)); // Should return true
console.log(isArmstrongNumber(371)); // Should return true
console.log(isArmstrongNumber(407)); // Should return true
console.log(isArmstrongNumber(123)); // Should return false
What is the application of Armstrong Numbers
We learned about Armstrong numbers in JavaScript and also learned how to verify if a number is an Armstrong number. Now we will know what is real-time use of the Armstrong number is.
- Educational Purposes – The Armstrong number is often used as an educational example to make students userland for loop as for loop is used in the Armstrong program.
- Puzzle Games – Armstrong’s numbers can be incorporated into puzzle games or brain teasers to create challenging number-based puzzles to find out Armstrong’s numbers and show the correct answers for multiple problems.
- Cryptography – In some cases, Armstrong numbers can be used in cryptographic algorithms or random number generation, although this is rare and typically not recommended due to their predictable nature.
Conclusion
We’ve learned what is Armstrong number in javascript, their unique properties, and how to check whether a number is an Armstrong number or not. Additionally, we’ve written programs in JavaScript code to find Armstrong numbers within a specified range. This is the most famous question asked in coding interviews for freshers.