0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Armstrong Number

To check an Armstrong number in JavaScript, calculate the sum of each digit raised to the power of the number of digits and compare it to the original number using code like function isArmstrong(num) { const digits = num.toString().length; let sum = 0; let temp = num; while (temp > 0) { let digit = temp % 10; sum += digit ** digits; temp = Math.floor(temp / 10); } return sum === num; }.
📋

Examples

Input153
Outputtrue
Input9474
Outputtrue
Input123
Outputfalse
🧠

How to Think About It

To check if a number is an Armstrong number, first find how many digits it has. Then, for each digit, raise it to the power of the total digits and add all these values together. If the sum equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
Initialize a sum to zero.
4
Extract each digit from the number.
5
Raise the digit to the power of the number of digits and add to sum.
6
Compare the sum with the original number and return true if equal, else false.
💻

Code

javascript
function isArmstrong(num) {
  const digits = num.toString().length;
  let sum = 0;
  let temp = num;
  while (temp > 0) {
    let digit = temp % 10;
    sum += digit ** digits;
    temp = Math.floor(temp / 10);
  }
  return sum === num;
}

console.log(isArmstrong(153));
console.log(isArmstrong(9474));
console.log(isArmstrong(123));
Output
true true false
🔍

Dry Run

Let's trace the number 153 through the code

1

Count digits

153 has 3 digits

2

Initialize sum

sum = 0, temp = 153

3

Extract digit and add power

digit = 153 % 10 = 3; sum = 0 + 3^3 = 27; temp = 15

4

Next digit

digit = 15 % 10 = 5; sum = 27 + 5^3 = 27 + 125 = 152; temp = 1

5

Next digit

digit = 1 % 10 = 1; sum = 152 + 1^3 = 152 + 1 = 153; temp = 0

6

Compare sum and number

sum = 153 equals original number 153, return true

tempdigitsum
153327
155152
11153
💡

Why This Works

Step 1: Count digits

We find the number of digits because each digit must be raised to this power.

Step 2: Sum of powers

Each digit is raised to the power of the digit count and added to a sum.

Step 3: Compare sum

If the sum equals the original number, it confirms the number is Armstrong.

🔄

Alternative Approaches

Using string and array methods
javascript
function isArmstrong(num) {
  const digits = num.toString().length;
  const sum = num.toString().split('').reduce((acc, d) => acc + Number(d) ** digits, 0);
  return sum === num;
}

console.log(isArmstrong(153));
This method is shorter and uses array functions but may be less clear for beginners.
Recursive approach
javascript
function powerSum(num, digits) {
  if (num === 0) return 0;
  return (num % 10) ** digits + powerSum(Math.floor(num / 10), digits);
}
function isArmstrong(num) {
  const digits = num.toString().length;
  return powerSum(num, digits) === num;
}

console.log(isArmstrong(153));
Uses recursion to sum powers, which is elegant but may be harder to understand for beginners.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The iterative approach is generally fastest and simplest, while recursive and array methods may add overhead.

ApproachTimeSpaceBest For
Iterative loopO(d)O(1)Simplicity and performance
Array reduceO(d)O(d)Concise code, functional style
RecursiveO(d)O(d)Elegant but uses call stack
💡
Convert the number to a string to easily count digits and access each digit.
⚠️
Forgetting to raise each digit to the power of the total number of digits instead of just cubing.