JavaScript Program to Check Armstrong Number

You are Here:

What is Armstrong Number?

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

153 = 13 + 53 + 33
153 = 1 + 125 + 27
153 = 153

Note: Each number is raised to the power of 3, because, the number of digits in 153 is 3.

Tips: It is recommended to use our online Armstrong Number calculator for better understanding.

Check Armstrong Number

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> var num = 19; var copyNum = num; var digits = 0; var remainder = 0; var total = 0; //find number of digits in num variable while(copyNum != 0) { digits++; copyNum = Math.floor(copyNum / 10); } copyNum = num; //slice the numbers from last digits while(copyNum != 0) { remainder = copyNum % 10; total += Math.pow(remainder, digits); copyNum = Math.floor(copyNum / 10); } // result if(num == total) document.write(num +" is an armstrong number"); else document.write(num +" is not an armstrong number"); </script> </body> </html>

Armstrong Numbers between the Given Range

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

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author