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:

- Count the number of digits in the given number.
- Take each digit of the number and raise it to the power of the total number of digits.
- Sum up the results of the previous step.
- 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.

## 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:

- Determine the number of digits by converting it to a string and using the length() method.
- Initialize variables for the original number and the sum of digit powers.
- Enter a loop to extract digits from the number using the modulus operator (%).
- Raise each digit to the power of the total number of digits using Math.pow().
- Add the result to the sum.
- Remove the last digit by dividing the number by 10.
- Repeat steps 3-6 until all digits have been processed (number becomes zero).
- 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:

- Convert the given number to a string using the valueOf() method.
- Determine the number of digits by getting the length of the string.
- Initialize variables to store the original number and the sum of digit powers.
- Iterate through each character in the string representation of the number.
- Convert each character back to its numeric value using the Character.getNumericValue() method.
- Raise the numeric value of each digit to the power of the total number of digits using the Math.pow() method.
- Add the result to the sum of digit powers.
- Repeat steps 4-7 for each digit in the number.
- 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:

- 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.
- Create an integer array of size equal to the number of digits.
- Initialize variables to store the original number and the sum of digit powers.
- Iterate through the digits in reverse order by starting from the last digit.
- Extract the last digit from the number using the modulus operator (%) by dividing it by 10.
- Store the extracted digit in the corresponding position of the array.
- Raise the extracted digit to the power of the total number of digits using the Math.pow() method.
- Add the result to the sum of digit powers.
- Remove the last digit from the number by dividing it by 10 (integer division).
- Repeat steps 5-9 until all digits have been processed.
- 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:

- Create a function, let’s call it
`isArmstrong`

, which takes the number to be checked and a sum variable as parameters. - 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. - If the number is not zero, extract the last digit of the number using the modulus operator (%) by dividing it by 10.
- Raise the extracted digit to the power of the total number of digits using the Math.pow() method.
- Add the result to the sum variable.
- Call the
`isArmstrong`

function recursively, passing the number divided by 10 (to remove the last digit) and the updated sum as arguments. - Repeat steps 3-6 until the base case is reached (number equals 0).
- Finally, outside the function, call the
`isArmstrong`

function with the original number and an initial sum of 0. - 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**