Signup/Sign In

JavaScript program to check Armstrong Number

An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits, each raised to the power of the digit count. In this program, we will use JavaScript to create a program that will determine whether a given number is an Armstrong number.

The program will take an input number and determine whether it equals the sum of each digit raised to the power of the total number of digits. This program will show you how to use JavaScript to manipulate numbers and test for specific properties, and it can be a useful tool for coding challenges and mathematical problem-solving. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

Armstrong Number

Any n-digit integer is said to be an Armstrong number if the sum of the n-th power of all the individual digits is the same as the number itself.

Example: Let's take the number, 153 -The sum of the 3rd power of all the digits of 153 is 1^3 + 5^3 + 3^313+53+33, which is equal to 1+125+27, which is similar to 153, which is the same as the number taken initially. Thus, the number 153 can be considered a 3-digit Armstrong number.

abcd = a^n + b^n + c^n + d^n

Let's take another example of 1634.

1634 = 1*1*1*1 + 6*6*6*6* + 3*3*3*3 + 4*4*4*4

Approach:

The JavaScript program will take an integer as an input and if the number has n digits in it, it is said to be an Armstrong number if the sum of the nth power of all the individual digits is the same as the input number itself. We will follow the next steps to check for Armstrong's number:

  • We will extract all the digits of the input integer one by one using the while loop.
  • We take the nth power of the digits and find the sum of these nth power of digits. At the end of the loop, when we see the input number is the same as the sum of those digits and power, it's an Armstrong Number, or else it's not.
  • The Time Complexity to find out an Armstrong Number is O(log(n)).

Program to check Armstrong Number

// JavaScript program to check an Armstrong number of n digits

const num = prompt("Enter an integer");
const digits = num.length;
var sum = 0;
var temp = num;

while (temp > 0) {

    var remainder = temp % 10;
    sum += (remainder ** digits);
    // removing the last digit from the number
    temp = parseInt(temp / 10); // convert float into integer
}

if (sum == num) 
    console.log(num + " is an Armstrong number");
else 
    console.log(num + " is not an Armstrong number");


Enter an integer: 8208
8208 is an Armstrong number

Conclusion

In conclusion, the Armstrong number is a distinct mathematical concept with distinct properties that set it apart. We were able to create a program that checks if a given number is an Armstrong number using JavaScript. The program shows how to use JavaScript's mathematical operations and control flow to test a number for the Armstrong property.

Developers can gain a deeper understanding of how to manipulate numbers and test for specific properties using JavaScript by understanding the logic and inner workings of the program.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.