Armstrong Number in JavaScript
Posted by Superadmin on May 02 2023 13:34:53

Armstrong Number in JavaScript

By Priya PedamkarPriya Pedamkar
  

Armstrong-Number-in-JavaScript

Overview of Armstrong Number in JavaScript

An Armstrong number is a number whose sum of cubes of all the digits in it will be equal to 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 the number if it is Armstrong 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. If the sum and original matches then, we can say the number is Armstrong number. Generally, it is a common paradigm to use or used widely the cube or three as power n, but that applies to 3-digit numbers only. This is because it is generally asked to check the 3-digit number if it is Armstrong or not. In a nutshell, 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 number. There are only four 3-digit Armstrong numbers are there. In case of other powers than 3 the n will vary. In 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 Number in JavaScript

Given below is the examples of Armstrong number in JavaScript:

Example #1: Using 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 out the number of digits in the number entered by the user so that this can be used to find out 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:

armstrong no in javascript 1

For an Invalid Armstrong number:

armstrong no in javascript 2JPG

Example #2: Using for loop

The same result can be achieved by using the for loop to find out the sum. In both cases, the logic to find out the Armstrong number will remain the same. This example is here to just 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 all the number of digits times till all the digits are processed.

Example 3

Conclusion

An Armstrong number is a number that is 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.