Algorithm


  1. Input:

    • Take the lower and upper bounds of the interval as input.
  2. Loop through the interval:

    • Start a loop that iterates through each number in the given interval.
  3. 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.
  4. 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.
  5. 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.
  6. 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

x
+
cmd
Enter a positive low integer value: 8
Enter a positive high integer value: 500
Armstrong Numbers:
8
9
153
370
371
407
Advertisements

Demonstration


JavaScript Programing Example to Find Armstrong Number in an Interval-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way