Algorithm
-
Input:
- Take the lower and upper bounds of the interval as input.
-
Loop through the interval:
- Start a loop that iterates through each number in the given interval.
-
Calculate the number of digits:
- Find the number of digits in the current number. You can do this by converting the number to a string and finding its length.
-
Calculate the sum of powers of digits:
- For each digit in the number, raise it to the power of the number of digits and add the result to a running sum.
-
Check for Armstrong number:
- Compare the sum calculated in the previous step with the original number. If they are equal, the number is an Armstrong number.
-
Output:
- Display or store the Armstrong numbers found in the interval.
Code Examples
#1 Code Example- Armstrong Numbers Between Two Intervals
Code -
Javascript Programming
// program to find Armstrong number between intervals
// take an input
const lowNumber = parseInt(prompt('Enter a positive low integer value: '));
const highNumber = parseInt(prompt('Enter a positive high integer value: '));
console.log ('Armstrong Numbers:');
// looping through lowNumber to highNumber
for (let i = lowNumber; i < = highNumber; i++) {
// converting number to string
let numberOfDigits = i.toString().length;
let sum = 0;
// create a temporary variable
let temp = i;
/* loop through a number to find if
a number is an Armstrong number */
while (temp > 0) {
let remainder = temp % 10;
sum += remainder ** numberOfDigits;
// removing last digit from the number
temp = parseInt(temp / 10); // convert float into integer
}
if (sum == i) {
console.log(i);
}
}
Copy The Code &
Try With Live Editor
Output
Enter a positive low integer value: 8
Enter a positive high integer value: 500
Armstrong Numbers:
8
9
153
370
371
407
Enter a positive high integer value: 500
Armstrong Numbers:
8
9
153
370
371
407
Demonstration
JavaScript Programing Example to Find Armstrong Number in an Interval-DevsEnv