25. JavaScript Program to find the given number is Armstrong number or Not.
How does this program work?
- In this program you are going to learn about to check if a number is Armstrong number or not using JavaScript.
- An armstrong number is a number whose value is equal to the sum of the cubes of its digits.
- If sum is equal to the original number, then that number is armstrong number else the number is not a armstrong number.
Here is the code
<html>
<head>
<title>JavaScript program to Check a given number is Armstrong number or Not</title>
</head>
<body>
<table>
<tr>
<td> <input type="text" name="a" id="first" placeholder="Enter a number"> </td>
</tr>
<tr>
<td> <button onclick="armstrong ()" >Submit</button> </td>
</tr>
</table>
<div id ="num"></div>
</body>
<script type="text/javascript">
function armstrong()
{
var r,n,sum = 0;
n = parseInt(document.getElementById ("first").value);
n1 = n;
while(n > 0)
{
r = n%10;
sum = sum+(r*r*r);
n = parseInt(n/10);
}
if(sum == n1)
{
document.getElementById ("num").innerHTML = n1+" is an Armstrong number ";
}
else
{
document.getElementById ("num").innerHTML = n1+" is not an Armstrong number";
}
}
</script>
</html>