# Armstrong Number in Java

How to Write Java Armstrong Number program using While Loop, For Loop, Functions, and Recursion?. We will also show the Java Armstrong Numbers between 1 to n.

## Java Armstrong Number

If the given number is equal to the sum of the power of n for each digit present in that integer then, that number can be Armstrong Number in Java. For example, 153 is Armstrong Number because of the Number of individual digits in 153 = 3 and

153 = 1³ + 5³ + 3³ ==> 1 + 125 + 27 ==> 153

The below steps will show the common approach to check for the Armstrong Number in Java

1. Enter any number and divide that number into individual digits (For Example, Divide 153 into 1, 5, and 3) and count them.
2. Calculate the power of n for each individual and add those numbers
3. Compare the original value with Sum value.
4. If they exactly matched, then it is Armstrong number else it is not Armstrong

## Java Armstrong Number using While Loop

This Armstrong Number in Java program allows the user to enter any positive integer. This program will check whether a number is Armstrong Number or Not using the Java While Loop

```// Armstrong Number in Java Programming

package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumberUsingWhile {
private static Scanner sc;

public static void main(String[] args) {
int Number, Temp, Reminder, Times = 0;
double Sum = 0;
sc = new Scanner(System.in);
System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();

//Helps to prevent altering the original value
Temp = Number;
while (Temp != 0) {
Times = Times + 1;
Temp = Temp / 10;
}

Temp = Number;
while( Temp > 0)  {
Reminder = Temp %10;
Sum = Sum + Math.pow(Reminder, Times);
Temp = Temp /10;
}
System.out.format("\n Sum of entered number is = %.2f", Sum);

if (Sum == Number) {
System.out.format("\n% d is a Armstrong Number", Number);
}
else {
System.out.format("\n% d is NOT a Armstrong Number", Number);
}
}
}```

In this Java Armstrong Number program, the following statement will ask the user to enter any positive integer. Then, that number assigned to a Number variable.

```System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();```

Next, We assign the original value to the Temp variable. It will help us to preserve our original value and then do all the manipulation on the Temp variable.

The below While loop will make sure that the given number is greater than 0, and statements inside the while loop will split the numbers and counts individual digits inside the given number. If you do not understand the logic, please refer to Java Program to Count Number Of Digits in a Number article.

```while (Temp != 0) {
Times = Times + 1;
Temp = Temp / 10;
}```

The second Java while loop will keep the given number greater than 0 and calculates the sum of the powers of the given number.

```while( Temp > 0)  {
Reminder = Temp %10;
Sum = Sum + Math.pow(Reminder, Times);
Temp = Temp /10;
}```

Let us see the working principle of this Java Armstrong Number while loop in iteration wise. Here, the user Entered value are Number = 9474 and Sum = 0

Temp = Number
Temp = 9474

First Iteration
Reminder = Temp %10
Reminder = 9474 % 10 = 4

Sum = Sum + Math.pow (Reminder, Times)

For this example, Times = 4 because number of digits in 9474 = 4. So, math.pow function will multiply the Reminder 4 times

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)
Sum = 0 + (4 * 4 * 4 * 4) = 0 + 256 = 256

Temp = Temp /10
Temp = 9474 /10 = 947

NOTE: If the number of digits count is 5, then Reminder will be multiplied by 5 times.

Second Iteration

From the first Iteration, the values of both Temp and Sum changed as Temp = 947 and Sum = 256

Reminder = Temp %10
Reminder = 947 % 10 = 7

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)
Sum = 256 + (7 * 7 * 7 * 7) => 256 + 2401
Sum = 2657

Temp = Temp /10 = 947 /10
Temp = 94

Third Iteration

From the Third Iteration, the values of Temp = 94 and Sum = 2657

Reminder = Temp %10
Reminder = 94 % 10 = 4

Sum = 2657 + (4 * 4 * 4 * 4) = >2657 + 256
Sum = 2913

Temp = Temp /10 = 94 /10
Temp = 9

Fourth Iteration: Temp = 9 and Sum = 2913

Reminder = Temp %10
Reminder = 9 % 10 = 9

Sum = 2913 + (9 * 9 * 9 * 9) => 2913 + 6561
Sum = 9474

Temp = Temp /10 = 9/10
Temp = 0

Here Number = 0. So, the while loop condition will fail

if ( Number == Sum ) – the condition will check whether the user enter number is exactly equal to Sum number or not. If this condition is True, then it is Armstrong else the given number is not Armstrong number in Java

NOTE: If you are looking for the Java Armstrong number below 1000, then remove the while loop to count the number of digits in a number. And then replace the below code

```Sum = Sum + Math.pow(Reminder, Times);

With

Sum = Sum + (Reminder * Reminder * Reminder)```

## Check Armstrong Number in Java using For Loop

This Java Armstrong Number program will check whether a number is Armstrong Number or Not using Java For Loop.

```// Check Armstrong Number in Java using For Loop

package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumberUsingFor {
private static Scanner sc;

public static void main(String[] args) {
int Number, Temp, Reminder, Times = 0;
double Sum = 0;
sc = new Scanner(System.in);
System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();

//Helps to prevent altering the original value
Temp = Number;
while (Temp != 0) {
Times = Times + 1;
Temp = Temp / 10;
}

for(Temp = Number; Temp > 0; Temp =  Temp /10 )  {
Reminder = Temp %10;
Sum = Sum + Math.pow(Reminder, Times);
}
System.out.format("\n Sum of entered number is = %.2f", Sum);

if (Sum == Number) {
System.out.format("\n% d is a Armstrong Number", Number);
}
else {
System.out.format("\n% d is NOT a Armstrong Number", Number);
}
}
}```

Java Armstrong Number using for loop output

``````
Please Enter number to Check for Armstrong:
1634

Sum of entered number is = 1634.00
1634 is a Armstrong Number``````

## Check Java Armstrong Number using Functions

This program for Armstrong Number in Java allows you to enter any positive integer. Then, this program will check whether a number is Armstrong Number or Not using Functions.

```// Check Armstrong Number in Java using FUnctions

package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumberUsingMethods {
private static Scanner sc;

public static void main(String[] args) {
int Number;
sc = new Scanner(System.in);
System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();
ArmstrongNumber(Number);
}
public static void ArmstrongNumber(int Number) {
int Temp, Reminder, Times = 0;
double Sum = 0;
//Helps to prevent altering the original value
Temp = Number;
while (Temp != 0) {
Times = Times + 1;
Temp = Temp / 10;
}

for(Temp = Number; Temp > 0; Temp =  Temp /10 )  {
Reminder = Temp %10;
Sum = Sum + Math.pow(Reminder, Times);
}
System.out.format("\n Sum of entered number is = %.2f", Sum);

if (Sum == Number) {
System.out.format("\n% d is a Armstrong Number", Number);
}
else {
System.out.format("\n% d is NOT a Armstrong Number", Number);
}
}
}```
`````` Please Enter number to Check for Armstrong:
153

Sum of entered number is = 153.00
153 is a Armstrong Number``````

When the Javac compiler reaches to ArmstrongNumber (Number) line in the main() program of Java Armstrong Number, then the compiler will immediately jump to below function:

`public static void ArmstrongNumber(int Number) {`

We already explained LOGIC in the above example.

## Check Armstrong Number in Java using Oops

In this program to find Armstrong Number in Java, we are dividing the code using the Object-Oriented Programming. To do this, first, we will create a class that holds a method to reverse an integer recursively.

```package FrequentPrograms;

public class ArmstrongNumbers {
int Times, Reminder;

public int Count_Numbers(int Number) {
while (Number != 0) {
Times = Times + 1;
Number = Number / 10;
}
return Times;
}

public double Check_Armstrong(int Number) {
double Sum = 0;
Times = Count_Numbers(Number);
while(Number > 0) {
Reminder = Number %10;
Sum = Sum + Math.pow(Reminder, Times);
Number = Number /10;
}
return Sum;
}

}```

Within the Main Armstrong program, we will create an instance and call the methods

```package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumberUsingClass {
private static Scanner sc;

public static void main(String[] args) {
int Number;
double Sum = 0;

sc = new Scanner(System.in);
System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();

ArmstrongNumbers arms = new ArmstrongNumbers();
Sum = arms.Check_Armstrong(Number);

System.out.format("\n Sum of entered number is = %.2f", Sum);

if (Sum == Number) {
System.out.format("\n% d is a Armstrong Number", Number);
}
else {
System.out.format("\n% d is NOT a Armstrong Number", Number);
}
}
}```
`````` Please Enter number to Check for Armstrong:
371

Sum of entered number is = 371.00
371 is a Armstrong Number``````

ArmstrongNumbers Class Analysis:

In this Java Armstrong Number program, we created one function to count the number of digits in a given number. Next, we declared one more function to calculate the sum of the power of n for each digit present in that integer. We already explained the Logic in the above example.

NOTE: With the Check_Armstrong(int Number) method, we are calling the Count_Number() function to get the Times value. If you want to pass the power value as an argument, then declare one more argument and remove this line.

Main Class Analysis:

First, we created an instance / created an Object of the ArmstrongNumbers Class

`ArmstrongNumbers arms = new ArmstrongNumbers();`

Next, we are calling the Check_Armstrong(Number) method.

`Sum = arms.Check_Armstrong(Number);`

Lastly, System.out.println statement will print the output.

## Check Armstrong Number in Java using Recursion

This Armstrong Number in Java program will check whether a number is Armstrong Number or Not using Recursion concept.

```package FrequentPrograms;

public class ArmstrongNumber {
int Reminder;
double Sum = 0;

public double Check_Armstrong(int Number, int Times) {
if (Number > 0) {
Reminder = Number %10;
Sum = Sum + Math.pow(Reminder, Times);
Check_Armstrong(Number /10, Times);
}
return Sum;
}
}```

Within the Main Armstrong program

```// Armstrong Number in Java Programming

package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumberUsingRecursion {

private static Scanner sc;

public static void main(String[] args) {
int Number, Times;
double Sum = 0;

sc = new Scanner(System.in);
System.out.println("\n Please Enter number to Check for Armstrong: ");
Number = sc.nextInt();

ArmstrongNumbers arms = new ArmstrongNumbers();
Times = arms.Count_Numbers(Number);

ArmstrongNumber arm = new ArmstrongNumber();
Sum = arm.Check_Armstrong(Number, Times);

System.out.format("\n Sum of entered number is = %.2f", Sum);

if (Sum == Number) {
System.out.format("\n% d is a Armstrong Number", Number);
}
else {
System.out.format("\n% d is NOT a Armstrong Number", Number);
}
}
}```

Output

`````` Please Enter number to Check for Armstrong:
370

Sum of entered number is = 370.00
370 is a Armstrong Number``````

In this Java Armstrong Number program, Within the function, Check_Armstrong(int Number, int Times), we are using the If statement to check whether the given number is greater than zero or not. It is very important, and if you forget this, then it ends up in an infinite loop.

`if (Number > 0) {`

We already explained the logic in our previous examples. Within the If statement, we used one 1 extra line.

`Check_Armstrong(Number /10, Times);`

The above Statement will help to call the function Recursively with the updated value. If we miss this statement, then after completing the first line, it will terminate. For example, Number = 153 then the output will be 27.

## Java Program to Find Armstrong Numbers between 1 to N

This Java Armstrong Number program allows the user to enter the minimum and maximum values. This program will find the Armstrong Numbers between the Minimum and Maximum values.

```// Java Program to Find Armstrong Numbers between 1 to n

package FrequentPrograms;

import java.util.Scanner;

public class ArmstrongNumbersbetween {
private static Scanner sc;

public static void main(String[] args) {
int Temp, Number, Minimum, Maximum, Reminder;
sc = new Scanner(System.in);

System.out.println("\n Please Enter the Minimum Value: ");
Minimum = sc.nextInt();

System.out.println("\n Please Enter the Maximum Value: ");
Maximum = sc.nextInt();

//ArmstrongNumbers arms = new ArmstrongNumbers();
for(Number = Minimum; Number <= Maximum; Number++) {
int sum = 0, Times = 0;
Temp = Number;
while (Temp > 0) {
Times = Times + 1;
Temp = Temp / 10;
}
Temp = Number;
while( Temp > 0)  {
Reminder = Temp %10;
sum = sum + (int)Math.pow(Reminder, Times);
Temp = Temp /10;
}
if(sum == Number) {
System.out.format(" %d  ", Number);
}
}
}
}```

Java Armstrong Numbers from 100 to 100000 output

`````` Please Enter the Minimum Value:
100

`for(Number = Minimum; Number <= Maximum; Number++) {`