Updated June 9, 2023

## Overview of Armstrong Number in JavaScript

An Armstrong number is a number whose sum of cubes of all the digits will equal itself. In general, it is also called a narcissistic number. The narcissistic number proposes that the number is said to be narcissistic or Armstrong’s number if the sum of all of its digits raised to the power n, where n is the number of digits, is equal to the number itself.

### Logic

To check whether the number is Armstrong’s number or not, we will check the criteria or the property to be filled by that number. We can calculate the sum of all the digits raised to the power of n from the number and then compare this sum with the original number. We can say the number is Armstrong if the sum and original match. Generally, it is a common paradigm to use or use the cube or three as power n widely, but that applies to 3-digit numbers only. This is because it is generally asked to check the 3-digit number to see if it is Armstrong or not. All three-digit numbers will have the power of 3 to be added to form Armstrong’s number.

Given below is an example of a three-digit number:

153 = (1)^{3} + (5)^{3} + (3)^{3}

= (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)

= 1 + 125 + 27

= 153

So, the number 153 is an Armstrong number.

123 = (1)3 + (2)3 + (3)3

= (1 * 1 * 1) + (2 * 2 * 2) + (3 * 3 * 3)

= 1 + 8 + 27

= 36

Which is not equal to the original number; therefore, this number is not Armstrong’s number. There are only four 3-digit Armstrong numbers are there. In case of other powers than 3, the n will vary. In the case of 4 digits, the power will be 4.

e.g. 1634 = (1)4 + (6)4 + (3)4 + (4)4

= (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)

= 1 + 1296 + 81 +256

= 1634

Here, as we have 4 digits, we have used 4 as power.

### Examples of Armstrong Numbers in JavaScript

Given below is the examples of Armstrong numbers in JavaScript:

#### Example #1: Using a while loop

**Code**:

```
<!DOCTYPE html>
<html>
<head>
<title>
Armstrong Number in JavaScript
</title>
<style>
.results {
border : green 1px solid;
background-color : aliceblue;
text-align : left;
padding-left : 20px;
height : 300px;
width : 95%;
}
.resultText {
font-size : 20px;
font-style : normal;
color : blue;
}
</style>
</head>
<body>
<div class = "results">
<h2> Enter the Number </h2>
Number: <input type = "number" name = "number" required id = "number">
<button type = "button" onclick = "checkArmstrong()" > Submit </button>
<div class = "resultText">
<p id = "result"> </p>
</div>
</div>
</div>
<script>
function checkArmstrong(){
num = document.getElementById("number").value;
var Number = num;
var digits = 0;
// Finding the number of digits
while(num > 0){
digits = digits + 1;
num = parseInt(num/10);
}
num = Number;
var sum = 0;
// calculating sum
while(num > 0) {
var digit = num%10;
sum = sum + Math.pow(digit, digits);
num = parseInt(num/10);
}
// checking sum with original number
if(sum == Number){
document.getElementById("result").style.color = "blue";
document.getElementById("result").innerHTML = "The number: " + Number + " is Armstrong Number";
}else{
document.getElementById("result").style.color = "red";
document.getElementById("result").innerHTML = "The number: " + Number + " is NOT Armstrong Number";
}
}
</script>
</body>
</html>
```

**Output**:

Here, we have generalized the formula to calculate the Armstrong number. First, we have found the number of digits in the number entered by the user so that this can be used to determine the power. We have used a while loop to extract digits from the number one by one and process them.

For a valid Armstrong number:

For an Invalid Armstrong number:

#### Example #2: Using for loop

We can achieve the same result using a for loop to find the sum. The logic to find the Armstrong number will remain the same in both cases. This example is here to showcase another way of implementation in JavaScript.

**Code**:

```
<!DOCTYPE html>
<html>
<head>
<title>
Armstrong Number in JavaScript
</title>
<style>
.results {
border : green 1px solid;
background-color : aliceblue;
text-align : left;
padding-left : 20px;
height : 300px;
width : 95%;
}
.resultText {
font-size : 20px;
font-style : normal;
color : blue;
}
</style>
</head>
<body>
<div class = "results">
<h2> Enter the Number </h2>
Number: <input type = "number" name = "number" required id = "number">
<button type = "button" onclick = "checkArmstrong()" > Submit </button>
<div class = "resultText">
<p id = "result"> </p>
</div>
</div>
</div>
<script>
function checkArmstrong(){
num = document.getElementById("number").value;
var Number = num;
var digits = 0;
// Finding the number of digits
while(num > 0){
digits = digits + 1;
num = parseInt(num/10);
}
num = Number;
var sum = 0;
// calculating sum
var i;
for (i = 0; i<digits; i++ ) {
var digit = num%10;
sum = sum + Math.pow(digit, digits);
num = parseInt(num/10);
}// checking sum with original number
if(sum == Number){
document.getElementById("result").style.color = "blue";
document.getElementById("result").innerHTML = "The number: " + Number + " is Armstrong Number";
}else{
document.getElementById("result").style.color = "red";
document.getElementById("result").innerHTML = "The number: " + Number + " is NOT Armstrong Number";
}
}
</script>
</body>
</html>
```

**Output:**

Here, for calculating the sum, we have used for loop. We can iterate through the digits until we have processed all of them.

### Conclusion

An Armstrong number is a number equal to the sum of its digits to the power of n, where n is the number of digits. We can check if the number is Armstrong or not using the same logic.

### Recommended Articles

This is a guide to Armstrong Number in JavaScript. Here we discuss the Overview, logic, and examples of Armstrong Number in JavaScript. You may also have a look at the following articles to learn more –