Armstrong Number in Java: Top 4 Simple ways

In this article, we will check for Armstrong number in Java using different methods.

What is Armstrong Number?

An Armstrong number is a number that is equal to the sum of its own digits, each raised to the power of the total number of digits.

Here’s how to determine if a number is an Armstrong number:

  1. Count the number of digits in the given number.
  2. Take each digit of the number and raise it to the power of the total number of digits.
  3. Sum up the results of the previous step.
  4. If the sum is equal to the original number, then it is an Armstrong number; otherwise, it is not.

For example, let’s consider the number 153:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Since the sum of the individual digits raised to the power of the number of digits equals the original number, 153 is an Armstrong number.

Armstrong numbers are named after Michael F. Armstrong, who introduced them in a 1969 issue of the Scientific American magazine. They are an interesting mathematical concept and are often used as coding exercises to demonstrate programming logic and looping constructs.

Armstrong Number in Java

Different ways to Check Armstrong number in Java

We can follow 4 different approaches to check if a given number is an Armstrong number in Java. The outputs show whether the given numbers are indeed Armstrong numbers or not.

Using Mathematical Calculation

To check for Armstrong number in Java using mathematical calculation follow the below steps:

  1. Determine the number of digits by converting it to a string and using the length() method.
  2. Initialize variables for the original number and the sum of digit powers.
  3. Enter a loop to extract digits from the number using the modulus operator (%).
  4. Raise each digit to the power of the total number of digits using Math.pow().
  5. Add the result to the sum.
  6. Remove the last digit by dividing the number by 10.
  7. Repeat steps 3-6 until all digits have been processed (number becomes zero).
  8. Check if the sum of digit powers equals the original number. If true, it is an Armstrong number.

Java Code

public class ArmstrongNumberChecker {
    public static boolean isArmstrong(int number) {
        int originalNumber = number;
        int digits = String.valueOf(number).length();
        int sum = 0;
        while (number > 0) {
            int digit = number % 10;
            sum += Math.pow(digit, digits);
            number /= 10;
        }
        return sum == originalNumber;
    }

    public static void main(String[] args) {
        int number = 153;
        boolean isArmstrong = isArmstrong(number);
        System.out.println(number + " is an Armstrong number? " + isArmstrong);
    }
}

Output

153 is an Armstrong number? true

Using String Conversion

Using String conversion to check for Armstrong number in Java involves converting the given number to a string and manipulating the individual digits. Here’s an explanation of the steps involved:

  1. Convert the given number to a string using the valueOf() method.
  2. Determine the number of digits by getting the length of the string.
  3. Initialize variables to store the original number and the sum of digit powers.
  4. Iterate through each character in the string representation of the number.
  5. Convert each character back to its numeric value using the Character.getNumericValue() method.
  6. Raise the numeric value of each digit to the power of the total number of digits using the Math.pow() method.
  7. Add the result to the sum of digit powers.
  8. Repeat steps 4-7 for each digit in the number.
  9. Finally, check if the sum of the digit powers is equal to the original number. If the two values match, the number is an Armstrong number; otherwise, it is not.

Java Code

public class ArmstrongNumberChecker {
    public static boolean isArmstrong(int number) {
        String numberString = String.valueOf(number);
        int digits = numberString.length();
        int sum = 0;
        for (int i = 0; i < digits; i++) {
            int digit = Character.getNumericValue(numberString.charAt(i));
            sum += Math.pow(digit, digits);
        }
        return sum == number;
    }

    public static void main(String[] args) {
        int number = 371;
        boolean isArmstrong = isArmstrong(number);
        System.out.println(number + " is an Armstrong number? " + isArmstrong);
    }
}

Output

371 is an Armstrong number? true

Using Array of Digits

Using an array of digits to check for Armstrong number in Java involves converting the given number into an array of its individual digits. Here’s an explanation of the steps involved:

  1. Determine the number of digits in the given number. This can be done by taking the logarithm (base 10) of the number and adding 1.
  2. Create an integer array of size equal to the number of digits.
  3. Initialize variables to store the original number and the sum of digit powers.
  4. Iterate through the digits in reverse order by starting from the last digit.
  5. Extract the last digit from the number using the modulus operator (%) by dividing it by 10.
  6. Store the extracted digit in the corresponding position of the array.
  7. Raise the extracted digit to the power of the total number of digits using the Math.pow() method.
  8. Add the result to the sum of digit powers.
  9. Remove the last digit from the number by dividing it by 10 (integer division).
  10. Repeat steps 5-9 until all digits have been processed.
  11. Finally, check if the sum of the digit powers is equal to the original number. If the two values match, the number is an Armstrong number; otherwise, it is not.

Java Code

public class ArmstrongNumberChecker {
    public static boolean isArmstrong(int number) {
        int originalNumber = number;
        int digits = (int) Math.log10(number) + 1;
        int[] digitArray = new int[digits];
        int sum = 0;
        for (int i = digits - 1; i >= 0; i--) {
            digitArray[i] = number % 10;
            number /= 10;
            sum += Math.pow(digitArray[i], digits);
        }
        return sum == originalNumber;
    }

    public static void main(String[] args) {
        int number = 9474;
        boolean isArmstrong = isArmstrong(number);
        System.out.println(number + " is an Armstrong number? " + isArmstrong);
    }
}

Output

9474 is an Armstrong number? true

Using Recursion

Using recursion to check for Armstrong number in Java involves a recursive function that calculates the sum of the digit powers. Here’s an explanation of the steps involved:

  1. Create a function, let’s call it isArmstrong, which takes the number to be checked and a sum variable as parameters.
  2. In the isArmstrong function, check if the number is equal to 0. If it is, return whether the sum is also 0. This serves as the base case for the recursive function.
  3. If the number is not zero, extract the last digit of the number using the modulus operator (%) by dividing it by 10.
  4. Raise the extracted digit to the power of the total number of digits using the Math.pow() method.
  5. Add the result to the sum variable.
  6. Call the isArmstrong function recursively, passing the number divided by 10 (to remove the last digit) and the updated sum as arguments.
  7. Repeat steps 3-6 until the base case is reached (number equals 0).
  8. Finally, outside the function, call the isArmstrong function with the original number and an initial sum of 0.
  9. If the returned result is equal to the original number, then the number is an Armstrong number; otherwise, it is not.

Java Code

public class ArmstrongNumberChecker {
    public static boolean isArmstrong(int number) {
        return isArmstrongRecursive(number, 0);
    }

    private static boolean isArmstrongRecursive(int number, int sum) {
        if (number == 0) {
            return number == sum;
        }
        int digit = number % 10;
        return isArmstrongRecursive(number / 10, sum + (int) Math.pow(digit, String.valueOf(number).length()));
    }

    public static void main(String[] args) {
        int number = 8208;
        boolean isArmstrong = isArmstrong(number);
        System.out.println(number + " is an Armstrong number? " + isArmstrong);
    }
}

Output

8208 is an Armstrong number? true

Conclusion

In conclusion, Armstrong number in Java is a fascinating mathematical concept where a number is equal to the sum of its own digits raised to the power of the total number of digits.

By leveraging various techniques such as mathematical calculations, string conversion, array manipulation, and recursion, we can effectively determine whether a given number is an Armstrong number in Java.

Understanding Armstrong number in Java and implementing algorithms to check for their presence allows us to explore the fascinating realm of number theory and exercise our programming skills.

Related Article

Simple Java Programs asked in Interviews

Leave a Reply

Your email address will not be published. Required fields are marked *