In the following example, we will check whether the number 19 is an Armstrong number or not.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<h1>JS Armstrong Number</h1>
<script>
varnum=19;
varcopyNum=num;
vardigits=0;
varremainder=0;
vartotal=0;
//find number of digits in num variablewhile(copyNum!=0)
{digits++;
copyNum=Math.floor(copyNum/10);
}
copyNum=num;
//slice the numbers from last digitswhile(copyNum!=0)
{remainder=copyNum%10;
total+=Math.pow(remainder, digits);
copyNum=Math.floor(copyNum/10);
}
// resultif(num==total)
document.write(num+" is an armstrong number");
elsedocument.write(num+" is not an armstrong number");
</script>
</body>
</html>
In the following example, we will find all the Armstrong numbers between 1 and 200.
Example
HTML Online Editor
<!DOCTYPE html>
<html>
<body>
<h1>JS Find All Armstrong Numbers</h1>
<script>
varstart=1;
varend=200;
varflag=0;
for(start=start; start<=end; start++)
{//find the number of digits in start variablecopyNum=start;
total=0;
digits=0;
remainder=0;
while(copyNum!=0)
{digits++;
copyNum=Math.floor(copyNum/10);
}copyNum=start;
//slice the start variable from last digitwhile(copyNum!=0)
{remainder=copyNum%10;
total+=Math.pow(remainder, digits);
copyNum=Math.floor(copyNum/10);
}// resultif((start==total) && (start!=0))
{if(flag==0)
{document.write("Armstrong numbers between "+start+" to "+end+":<br>");
flag=1;
}
document.write(start+", ");
}
}
if(flag==0)
document.write("There is no armstrong numbers between the given range");
</script>
</body>
</html>