Armstrong Number in Javascript

Q. Write a program in javascript to check given number is Armstrong or not ? 

Ans.  An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number

example -  153 = 1**3 + 5**3 +  3**3 = 1+125+27 => 153

Armsrong Number program in javascript


<!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










Comments

Popular posts from this blog

C program that contains a string XOR each character in this string with 0 ,127

Queue in Data Structure(DS)

Implementation of stack Using Array