Home >>Javascript Programs >JavaScript code to check number is Armstrong or not

JavaScript code to check number is Armstrong or not

JavaScript code to check number is Armstrong or not

In this tutorial we will learn how to check a given number is Armstrong number or not. First, we will discuss what is Armstrong?

An Armstrong number of three digits is an integer when its value is equal to its sum of cube of digits to the number itself. An Armstrong number is always in a three-digit number. Let’s understand an Armstrong by given example – Here is a value 370. If you want to know 370 is Armstrong number or not. For this you need to cube the single number means (3)3, (7)3 and (0)3 , after cubing the numbers add all the numbers which are equal to 370.it means 370 is an Armstrong number.

For Example:-

370 = (3)3 + (7)3 +(0)3

370 = 27 + 343 + 0

370 = 370

Let's take an example:

<!doctype html>
<html>
<head>
<script>
function armstr()
{
var arm=0,a,b,c,d,num;
num=Number(document.getElementById("no_input").value);

temp=num;
while(temp>0)
{
a=temp%10;
temp=parseInt(temp/10); // convert float into Integer
arm=arm+a*a*a;
}
if(arm==num)
{
alert("Armstrong number");
}
else
{
alert("Not Armstrong number");
}
}
</script>
</head>
<body>
Enter any Number: <input id="no_input">
<button onclick="armstr()">Check</button></br></br>
</body>
</html>
Output:

Enter any Number: