Algorithm
- Start
- Accept the lower and upper limits from the user.
- Loop through each number in the given range (from lower limit to upper limit). a. Initialize sum to 0. b. Calculate the number of digits in the current number. c. Temporarily store the current number in a variable. d. Repeat until the temporary number becomes 0: i. Extract the last digit. ii. Raise the extracted digit to the power of the number of digits. iii. Add the result to the sum. iv. Remove the last digit from the temporary number. e. If the sum is equal to the original number, print it as an Armstrong number.
- End
Code Examples
#1 Code Example- Armstrong Numbers Between Two Integers
Code -
Java Programming
class Main {
public static void main(String[] args) {
int low = 999, high = 99999;
for(int number = low + 1; number < high; ++number) {
int digits = 0;
int result = 0;
int originalNumber = number;
// number of digits calculation
while (originalNumber != 0) {
originalNumber /= 10;
++digits;
}
originalNumber = number;
// result contains sum of nth power of its digits
while (originalNumber != 0) {
int remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
if (result == number) {
System.out.print(number + " ");
}
}
}
}
Copy The Code &
Try With Live Editor
Output
1634 8208 9474 54748 92727 93084
Demonstration
Java Programing Example to Display Armstrong Number Between Two Intervals-DevsEnv