Java program to check armstrong number

www‮.‬theitroad.com

Here's a Java program to check if a number is an Armstrong number or not:

public class ArmstrongNumber {
    public static void main(String[] args) {
        int number = 153;
        if (isArmstrongNumber(number)) {
            System.out.println(number + " is an Armstrong number.");
        } else {
            System.out.println(number + " is not an Armstrong number.");
        }
    }

    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 number that we want to check for being an Armstrong number. We then call a function called isArmstrongNumber to check if the number is an Armstrong number or not. If the function returns true, we print a message indicating that the number is an Armstrong number, and if the function returns false, we print a message indicating that the number is not an Armstrong number.

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 value of number to check for Armstrong numbers of different values.