Java program to display armstrong number between two intervals
Here's a Java program to display Armstrong numbers between two intervals:
public class ArmstrongNumber { public static void main(String[] args) { int low = 100; int high = 1000; System.out.printf("Armstrong numbers between %d and %d are: ", low, high); for (int i = low; i < high; i++) { if (isArmstrongNumber(i)) { System.out.print(i + " "); } } } public static boolean isArmstrongNumber(int number) { int sum = 0; int originalNumber = number; int digits = String.valueOf(number).length(); while (number > 0) { int remainder = number % 10; sum += Math.pow(remainder, digits); number /= 10; } return sum == originalNumber; } }
In this program, we first define the lower and upper limits of the interval we want to check for Armstrong numbers. We then use a for loop to check each number in this interval using a function called isArmstrongNumber
. If the function returns true for a particular number, we print that number to the console.
The isArmstrongNumber
function takes an integer as input and returns a boolean indicating whether the input number is an Armstrong number or not. The function first calculates the number of digits in the input number, and then uses a while loop to extract each digit and raise it to the power of the number of digits. The sum of these powers is compared to the original number to determine whether it is an Armstrong number.
You can modify the values of low
and high
to check for Armstrong numbers in different intervals.