Armstrong Number in Java

This post shows 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.

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

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

The steps below show the standard approach to checking for the Java Armstrong Number.

  1. Enter any number and divide it 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 the Sum value.
  4. If they match exactly, it is a Java Armstrong number; otherwise, it is not.

Java Armstrong Number using While Loop

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

import java.util.Scanner;
public class Example {	
	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 : ");
		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 is = %.2f", Sum);
		
		if (Sum == Number) {
			System.out.format("\n% d is a Armstrong", Number);
		}
		else {
			System.out.format("\n% d is NOT", Number);
		}
	}
}
Armstrong Number in Java using While Loop

In this Java Armstrong Number program, the first two statements will ask the user to enter any positive integer and assign it to a variable.

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 first While loop will ensure that the given number is greater than 0, and statements inside the while loop will split the numbers and count individual digits inside the given value. If you do not understand the logic, please refer to the Count Number Of Digits in a Number article.

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

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

Temp = Number = 9474

First Iteration of Java Armstrong Number
Reminder = Temp %10
Reminder = 9474 % 10 = 4

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

For this example, Times = 4 because the 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

If the digits count is 5, Reminder will be multiplied by 5 times.

2nd Iteration of Java Armstrong number program: From the first iteration, the values of both Temp and Sum changed as Temp = 947 and Sum = 256.

  • Reminder = 947 % 10 = 7
  • Sum = 256 + (7 * 7 * 7 * 7) => 256 + 2401 = 2657
  • Temp = 947 /10 = 94

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

  • Reminder = 94 % 10 = 4
  • Sum = 2657 + (4 * 4 * 4 * 4) = >2657 + 256 = 2913
  • Temp = 94 /10 = 9

4th Iteration: Temp = 9 and Sum = 2913

  • Reminder = 9 % 10 = 9
  • Sum = 2913 + (9 * 9 * 9 * 9) => 2913 + 6561 = 9474
  • Temp = 9/10 = 0

Here, Temp= 0. So, the Java Armstrong number programs while loop condition will fail

if ( Number== Sum ) – the condition will check whether the user enters one that is exactly equal to Sum or not. If this condition is True, it is Armstrong Number; otherwise, it is not.

NOTE: Remove the while loop to count the digits if you are looking for numbers below 1000. And then replace the code below.

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

With

Sum = Sum + (Reminder * Reminder * Reminder)

Java Program to Check Armstrong Number using For Loop

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

package FrequentPrograms;

import java.util.Scanner;

public class Example {
	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 : ");
		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);
		}
		else {
			System.out.format("\n% d is NOT", Number);
		}
	}
}
Armstrong Number in Java using For Loop

Java Program to Check Armstrong Number using Functions

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

package FrequentPrograms;

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Num;
		sc = new Scanner(System.in);		
		System.out.println("\n Please Enter integer to Check : ");
		Num = sc.nextInt();
		ArmNum(Num);
	}
	public static void ArmNum(int Num) {
		int Temp, Reminder, Times = 0;
		double Sum = 0;
		//Helps to prevent altering the original value
		Temp = Num;
		while (Temp != 0) {
			Times = Times + 1;
			Temp = Temp / 10;
		   }
		   
		for(Temp = Num; Temp > 0; Temp =  Temp /10 )  {
			Reminder = Temp %10;
		    Sum = Sum + Math.pow(Reminder, Times);
		   }
		System.out.format("\n Sum = %.2f", Sum);
		
		if (Sum == Num) {
			System.out.format("\n% d is a Armstrong", Num);
		}
		else {
			System.out.format("\n% d is NOT", Num);
		}
	}
}
Armstrong Number in Java using Funcations

When the Javac compiler reaches the ArmNum(Num) line in the main() program, the compiler will immediately jump to the below function:

public static void ArmNum(int Num) {

Using OOPS

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

package FrequentPrograms;

public class ArsNum {
	int Times, Reminder; 		

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

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

package FrequentPrograms;

import java.util.Scanner;

public class Example {
	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 : ");
		Number = sc.nextInt();

		ArsNum arms = new ArsNum();
		Sum = arms.Check_Arm(Number);   
		
		System.out.format("\n Sum of entered is = %.2f", Sum);
		
		if (Sum == Number) {
			System.out.format("\n% d is a Armstrong", Number);
		}
		else {
			System.out.format("\n% d is NOT", Number);
		}
	}
}
 Please Enter number to Check : 
371

 Sum of entered is = 371.00
 371 is a Armstrong

In this Java Armstrong Number program, we created one function to count the digits in it. 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_Arm(int num) method, we call the Count_Number() function to get the Times value. If you want to pass the power value as an argument, declare one more argument and remove this line.

Main Class Analysis:

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

ArsNum arms = new ArsNum();

Next, we are calling the Check_Arm method.

Sum = arms.Check_Arm(Number);

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

Java Program to Check Armstrong Number using Recursion

This Java program will check whether an Armstrong number uses the Recursion or recursive function concept.

package FrequentPrograms;

public class ArNu {
	int Reminder; 		
	double Sum = 0;
	
	public double Check_Arm(int Num, int Times) {
		if (Num > 0) {
			Reminder = Num %10;
			Sum = Sum + Math.pow(Reminder, Times);
			Check_Arm(Num /10, Times);
		}
		return Sum;
	}
}

Within the Main program

package FrequentPrograms;

import java.util.Scanner;

public class Example {

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

		ArNu arms = new ArNu();
		Times = arms.Count_Numbers(Num);   
		
		ArNu arm = new ArNu();
		Sum = arm.Check_Arm(Num, Times); 
		
		System.out.format("\n Sum of entered is = %.2f", Sum);
		
		if (Sum == Num) {
			System.out.format("\n% d is a Armstrong", Num);
		}
		else {
			System.out.format("\n% d is NOT", Num);
		}
	}
}
 Please Enter Value to Check : 
370

 Sum of entered is = 370.00
 370 is a Armstrong

In this Java Armstrong Number program, Within the function, Check_Arm(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; if you forget this, it ends up in an infinite loop.

if (Num > 0) {

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

Check_Arm(Number /10, Times);

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

Java Program to Print Armstrong Numbers between 1 to N

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

The For Loop helps the Javac compiler iterate between Minimum and Maximum Variables. The iteration starts at the Minimum, and then it will not exceed the Maximum variable.

import java.util.Scanner;
public class Example {	
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Temp, i, Minimum, Maximum, Reminder;
		sc = new Scanner(System.in);	
		
		System.out.println("\n Please Enter the Minimum : ");
		Minimum = sc.nextInt();
		
		System.out.println("\n Please Enter the Maximum : ");
		Maximum = sc.nextInt();
		
		for(i = Minimum; i <= Maximum; i++) {
			int sum = 0, Times = 0;
			Temp = i;
			while (Temp > 0) {
				Times = Times + 1;
				Temp = Temp / 10;
			}
			Temp = i;
			while( Temp > 0)  {
				Reminder = Temp %10;
				sum = sum + (int)Math.pow(Reminder, Times);
				Temp = Temp /10;
			}
			if(sum == i) {
				System.out.format(" %d  ", i);	
			}
		}
	}
}

From 1 to 10000 output

Java Program to Find Armstrong Number between 1 to N