Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
Armstrong Number in JavaScript (6 Programs)
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example:
-
153 is an Armstrong number because
-
9474 is an Armstrong number because
This concept is commonly used in programming to demonstrate loops, conditions, and mathematical operations. In this tutorial, we will explore how to implement programs to check the Armstrong number in JavaScript. Whether you're writing an Armstrong number in js for a single number or a range, this guide has all the examples.
Armstrong Number in JavaScript Using for Loop
This program checks whether a given number is an Armstrong number using a for loop.
Code
function isArmstrongNumber(number) {
const digits = number.toString().split("").map(Number);
const power = digits.length;
let sum = 0;
for (let i = 0; i < digits.length; i++) {
sum += Math.pow(digits[i], power);
}
return sum === number;
}
// Test the function
const num = 153;
console.log(`${num} is ${isArmstrongNumber(num) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
153 is an Armstrong number.
Armstrong Number in JavaScript Using Function
This version encapsulates the logic in a reusable function.
Code
function checkArmstrongNumber(number) {
let sum = 0;
let temp = number;
const power = number.toString().length;
while (temp > 0) {
const digit = temp % 10;
sum += Math.pow(digit, power);
temp = Math.floor(temp / 10);
}
return sum === number;
}
// Test the function
const testNum = 9474;
console.log(`${testNum} is ${checkArmstrongNumber(testNum) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
9474 is an Armstrong number.
Armstrong Number Program in JavaScript Using while Loop
This program demonstrates how to check if a number is an Armstrong number using a while loop for processing its digits.
Code
function isArmstrongUsingWhile(number) {
let sum = 0;
let temp = number;
const power = number.toString().length;
while (temp > 0) {
const digit = temp % 10;
sum += Math.pow(digit, power);
temp = Math.floor(temp / 10);
}
return sum === number;
}
// Test the function
const num2 = 370;
console.log(`${num2} is ${isArmstrongUsingWhile(num2) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
370 is an Armstrong number.
Armstrong Number in JS Using toString and split Methods
This program uses the toString method to convert a number to a string and the split method to break it into individual digits for processing.
Code
function isArmstrongUsingSplit(number) {
const digits = number.toString().split("").map(Number);
const power = digits.length;
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, power), 0);
return sum === number;
}
// Test the function
const num3 = 407;
console.log(`${num3} is ${isArmstrongUsingSplit(num3) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
407 is an Armstrong number.
Armstrong Number in JS Using Array.from Method
This method converts the number into an array of digits using Array.from for easier manipulation and calculation.
Code
function isArmstrongUsingArrayFrom(number) {
const digits = Array.from(String(number), Number);
const power = digits.length;
const sum = digits.reduce((acc, digit) => acc + Math.pow(digit, power), 0);
return sum === number;
}
// Test the function
const num4 = 9474;
console.log(`${num4} is ${isArmstrongUsingArrayFrom(num4) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
9474 is an Armstrong number.
Armstrong Number in JS Using Array.reduce Method
This approach leverages the reduce method to compute the sum of digits raised to the power of the number of digits in a concise way.
Code
function isArmstrongUsingReduce(number) {
const digits = number.toString().split("").map(Number);
const power = digits.length;
return digits.reduce((acc, digit) => acc + Math.pow(digit, power), 0) === number;
}
// Test the function
const num5 = 153;
console.log(`${num5} is ${isArmstrongUsingReduce(num5) ? "an Armstrong" : "not an Armstrong"} number.`);
Output
153 is an Armstrong number.
Armstrong Number in JavaScript for a Range of Numbers
This program finds and lists all Armstrong numbers within a given range by iterating through the range and checking each number.
Code
function findArmstrongNumbersInRange(start, end) {
const armstrongNumbers = [];
for (let i = start; i <= end; i++) {
if (isArmstrongNumber(i)) {
armstrongNumbers.push(i);
}
}
return armstrongNumbers;
}
// Test the function
const startRange = 100;
const endRange = 999;
console.log(`Armstrong numbers between ${startRange} and ${endRange}:`, findArmstrongNumbersInRange(startRange, endRange));
Output
Armstrong numbers between 100 and 999: [153, 370, 371, 407]
Explanation
Concepts Used in Above Programs
A. Iteration
for and while loops are used to process digits of the number and iterate through ranges.
B. Mathematical Operations
Operations like Math.pow and % are used to calculate powers and extract digits.
C. Array and String Manipulation
Numbers are converted to strings and split into digits for easier processing in some examples.
D. Functions
Reusable functions improve code readability and reusability for checking Armstrong numbers.