Armstrong Number in JavaScript

An Armstrong number is a distinct type of integer characterized by the property that the sum of its digits, each raised to the power corresponding to the total number of digits, equals the integer itself. For example, the number 153 qualifies as an Armstrong number because 1^3 + 5^3 + 3^3 results in 153.

Definition of Armstrong Number

An Armstrong number refers to a numerical value for which the sum of the cubes of its individual digits matches the number itself. This type of number is also known as a narcissistic number. Specifically, a number is classified as a narcissistic or Armstrong number if the sum of the digits raised to the power of n (where n indicates the total count of the digits) is equal to the number itself.

We can determine if a given number qualifies as an Armstrong number by utilizing a straightforward JavaScript program. This program will accept an integer as input and inform us whether the number is indeed an Armstrong number. The following sections will outline the steps to develop this program in JavaScript.

The Logic of Armstrong Number in JavaScript

In the JavaScript implementation designed to verify if a number qualifies as an Armstrong number, we will receive an integer as input. Based on the aforementioned definition of an Armstrong number, for a given number that consists of n digits, it can be classified as an Armstrong number if the sum of each digit raised to the power of n equals the original number itself. Consequently, the approach to determine if a number is an Armstrong number can be articulated as follows:

We will extract each digit from the provided number utilizing JavaScript loops. Subsequently, we will compute the nth power of each extracted digit and accumulate the results. If the resultant sum matches the original number that was input, then it is classified as an Armstrong number. Conversely, if the sum does not equal the entered number, it is not considered an Armstrong number. Regarding time complexity, verifying whether an integer 'n' qualifies as an Armstrong number has a time complexity of O(log(n)).

Presented below is the flowchart for the JavaScript application designed to determine whether a given number qualifies as an Armstrong number.

An example of a three-digit number is 153:

Example

153 = (1)³ + (5)³ + (3)³ 
= (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3) 
= 1 + 125 + 27 
= 153

Thus, 153 is classified as an Armstrong number.

Now consider

Example

123: 123 = (1)³ + (2)³ + (3)³ 
= (1 * 1 * 1) + (2 * 2 * 2) + (3 * 3 * 3) 
= 1 + 8 + 27 
= 36

Since this value does not match the original figure, 123 does not qualify as an Armstrong number. There are only four Armstrong numbers that consist of three digits. For any other powers, the value of n will vary. In the case of a four-digit number, the exponent will be 4.

For example, 1634 can be calculated as follows:

Example

1634 = (1)⁴ + (6)⁴ + (3)⁴ + (4)⁴
= (1 * 1 * 1 * 1) + (6 * 6 * 6 * 6) + (3 * 3 * 3 * 3) + (4 * 4 * 4 * 4)
= 1 + 1296 + 81 + 256
= 1634.

In this instance, since we are dealing with 4 digits, we have utilized 4 as the exponent.

Examples of Armstrong Numbers in JavaScript

Below are several illustrations of Armstrong numbers coded in JavaScript:

Example 1: Using a while loop

Example

<!DOCTYPE html>
<html>
<head>
<title>
Example for Armstrong Number in JavaScript
</title>
<style>
.results {
border : red 2px solid;
background-color : pink;
text-align : left;
padding-left : 15px;
height : 200px;
width : 90%;
}
.resultText {
font-size : 26px;
font-style : calibre;
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 = "green";
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>

In this segment, we have extended the formula used to identify an Armstrong number. Initially, we determine how many digits are present in the user-provided number, which informs us about the exponent needed for our calculations. We employ a while loop to extract each digit of the number sequentially, followed by executing the necessary computations.

Output

For a valid Armstrong number:

For an Invalid Armstrong number:

Example 2: Using for loop

We can utilize a for loop to calculate the sum as well. The approach to identify the Armstrong number will be consistent in both scenarios. This serves merely as an illustration of a different implementation in JavaScript.

Example

<!DOCTYPE html>
<html>
<head>
<title>
Example for Armstrong Number in JavaScript using For loop
</title>
<style>
.results {
border : violet 2px solid;
background-color : lightblue;
text-align : left;
padding-left : 15px;
height : 200px;
width : 90%;
}
.resultText {
font-size : 15px;
font-style : italian;
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 = "green";
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>

In this illustration, we have utilized a for loop to calculate the sum. This allows us to iterate through every single digit continuously until we have processed all of them.

Output

For a valid Armstrong number:

For an invalid Armstrong number:

Conclusion

  • A n-digit number is an Armstrong number if the sum of the n-th powers of its digits is equal to the number.
  • You can figure out whether a number is an Armstrong number or not by using a simple JavaScript program.
  • The time complexity for checking if an integer 'n' is an Armstrong number is O(log(n)).

Input Required

This code uses input(). Please provide values below: