HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JS Find All Armstrong Numbers</h1> <script> var start = 1; var end = 200; var flag = 0; for(start = start; start <= end; start++) { //find the number of digits in start variable copyNum = start; total = 0; digits = 0; remainder = 0; while(copyNum != 0) { digits++; copyNum = Math.floor(copyNum / 10); } copyNum = start; //slice the start variable from last digit while(copyNum != 0) { remainder = copyNum % 10; total += Math.pow(remainder, digits); copyNum = Math.floor(copyNum / 10); } //result if((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>
OUTPUT
×

Save as Private