JavaScript program to find Armstrong Number in an Interval
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. For instance, the number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
In this program, we will create a JavaScript function that takes two parameters, a start and an end, and finds all Armstrong numbers within the specified interval (inclusive). The function will iterate through the range of numbers, checking whether or not each number is an Armstrong number. Finally, the function will return an array containing all of the Armstrong numbers found in the specified interval.
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 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 2 integers as input, the upper and the lower and now we follow the same routine of checking if the number is an Armstrong number or not. 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 start the Outer loop with the lower number and then go toward the upper number.
- Now, in the Inner loop, 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. If it's an Armstrong Number we print the number else we skip it.
- The Time Complexity to find out an Armstrong Number is O((higher -lower)log(n)).
Program to find Armstrong Number in an Interval
// JavaScript program to find Armstrong numbers between intervals
const lower = prompt('Enter the lower integer value: ');
const higher = prompt('Enter the upper integer value: ');
console.log ('Armstrong Numbers are as follows :');
// looping through lower to higher
for (var i = lower; i < higher+1; i++) {
// converting number to string
var digits = i.toString().length;
var sum = 0;
var temp = i;
while (temp > 0) {
var remainder = temp % 10;
sum += remainder ** digits;
// removing last digit from the number
temp = parseInt(temp / 10); // convert float into integer
}
if (sum == i)
console.log(i);
}
Enter the lower integer value: 8
Enter the higher integer value: 500
Armstrong Numbers are as follows:
8
9
153
370
371
407
Conclusion
Finally, we created a JavaScript function that finds all Armstrong numbers within a given interval. The function takes two parameters, a start and an end value, and iterates through the number range. It determines whether a number is an Armstrong number by comparing the sum of its own digits raised to the power of the number of digits to the number itself. The function then returns an array containing all of the Armstrong numbers found in the specified interval.