In this article, we will understand what an Armstrong Number in java is. We will see the Approach to check if a number is Armstrong or not along with its code.
We recommend the following topics of Java for a better understanding of the program.
 The basic syntax of Java.
 Loops in Java.
 Decision statement in Java.
What is an Armstrong Number in java?
A number is thought of as an Armstrong number in java if the sum of its own digits raised to the power number of digits gives the number itself. For example, 0, 1, 153, 370, 371, 407, 1634, 8208, and 9474 are threedigit Armstrong numbers and fourdigit Armstrong numbers too. There are many more. Let’s consider 407, as it is a threedigit number so it will be expressed as:
(407) —> (4 4 4) + (0 0 0) + (7 7 7)
64 + 0 + 343
=> 407
As 407 is a sum of the cube of its digits, therefore it is an Armstrong number.
Implementation of Armstrong Number Program in java
First, we have to declare the class AmstrongNum.Then declare the variables. Create an object of the scanner class as sc and read the number from the user into the variable num. Assign originalNum=num. Find the sum of cubes of digits of num by using a while loop with condition num != 0,
 Take each digit by finding the remainder of num divided by 10, digit=num
 Find the cube of the digit using the pow() method of Java, add it with cube_sum
 To find the next digit we’ll assign the num variable = num/10.
After exiting from the loop, check if cube_sum is the same as originalNum, if yes then display the number is Armstrong number, else display the number as not Armstrong number using the system.out.println().
ALGORITHM of Armstrong Number Program
 Declare the class ArmstrongNum with a public modifier.
 To start the program open the main(), Java program execution starts with the main()
 Declare the integer variables originalNum, digit, cubeSum, num
 Assign cubeSum = 0
 Create an object of the scanner class
 Read the number into the variable num
 Assign originalNum=num
 By using a while loop with the condition originalNum !=0, do steps 9,10
 Find the digit, the cube of that digit, and the sum of cubes into cubeSum
 Then to find the next digit divide the num by 10
 After processing the loop, check if cubeSum is equal to originalNum
 if true then display the number as Armstrong number
 else display the number is not Armstrong.
import java.util.Scanner; class ArmstrongNum { public static void main(String[] args) { int originalNum, digit, cubeSum = 0,num; System.out.println("Enter the number:"); Scanner sc = new Scanner(System. in ); num = sc.nextInt(); originalNum = num; while (num!= 0) { digit = num % 10; cubeSum += Math.pow(digit, 3); num /= 10; } if(cubeSum == originalNum) System.out.println(originalNum+ " is an Armstrong number"); else System.out.println(originalNum+ " is not an Armstrong number"); } }
Dry Running The Armstrong number Code:
Let input is equal to 153
variable values before for loop
num=153; check=153; Armstrong=0; result=0;
values of the variable in for loop are line by line
for i=1
Armstrong=3;
num=15;
Armstrong=333=27
result=0+27=27;
for loop condition check: num is not equal to zero loops will run again
for i=2
Armstrong=5;
num=1;
Armstrong=555=125
result=27+125=152;
for loop condition: num is not equal to zero loops will run again
for i=3
Armstrong=1;
num=0;
Armstrong=111=1;
result=152+1=153;
In for loop condition, the num is EQUAL TO ZERO therefore, the loop will run again
The loop will break and if else condition will be checked as or result=153 and check=153
if the condition will true program will show the output
153 is an Armstrong Number
Finding the Armstrong Number Using Static Method Between 100 and 999
Given below is the first sample program using the static method with sample output as well. Here we used the method ‘ Static ‘ and took numbers between 100 to 999. Once you have done the execution, it automatically displays the Armstrong numbers between 100 and 999. Check out the sample output.
import java.util.Scanner; class ArmstrongWhile { public static void main(String[] arg) { int i=100,arm; System.out.println("Armstrong numbers between 100 to 999"); while(i<1000) { arm=armstrongOrNot(i); if(arm==i) System.out.println(i); i++; } } static int armstrongOrNot(int num) { int x,a=0; while(num!=0) { x=num%10; a=a+(x*x*x); num/=10 ; } return a; } }
Checking Armstrong Number Using While Loop ( 1 to 500 )
There you go another method using a while loop. Java, a while loop is nothing but executing a set of statements repeatedly as long as the condition is true – here is the complete guide on a while loop in Java with examples. For this case, we have taken the example of numbers from 1 to 500. Upon execution, you will get to know what Armstrong numbers are. Check out the output below so that you will get an idea.
class ArmstrongWhile { public static void main(String[] arg) { int i=1,a,arm,n,temp; System.out.println("Armstrong numbers between 1 to 500 are"); while(i<500) { n=i; arm=0; while(n>0) { a=n%10; arm=arm+(a*a*a); n=n/10; } if(arm==i) System.out.println(i); i++; } } }
Find the Armstrong Number Using Recursion ( Between 1 to 1000 )
Another method using recursion: A function that calls itself is called recursion. Here is the complete code with the sample program and the output of the program. Here we have taken the example of numbers from 1 to 1000. Check it out.
import java.util.Scanner; class ArmstrongRecursion { int x; int findArmstrong(int n,int a) { if(n!=0) { x=n%10; a=a+(x*x*x); n/=10 ; return findArmstrong(n,a); } return a; } public static void main(String[] arg) { ArmstrongRecursion A=new ArmstrongRecursion(); int arm; System.out.println("Armstrong numbers between 1 to 1000"); for(int num=1;num<500;num++) { arm=A.findArmstrong(num,0); if(arm==num) System.out.println(num); } } }
Checking Armstrong Number Using for loop ( 100 to 500 )
Another sample program to print Armstrong numbers using a for a loop. Look out for the program given below.
import java.util.Scanner; class Armstrong { public static void main(String[] arg) { int a,arm=0,n,temp; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); temp=n; for( ;n!=0;n/=10 ) { a=n%10; arm=arm+(a*a*a); } if(arm==temp) System.out.println(temp+" is a armstrong number "); else System.out.println(temp+" is not a armstrong number "); } }
Armstrong Number In Java – FAQs

Which companies asked for Armstrong Number in java in an interview?
Companies like Gemini Solution, TCS, Infosys, and Unthinkable Solutions had recently asked for Armstrong Number in their interview round. 
What is the Armstrong Number in Java?
The Armstrong number is defined as three digits in an integer, where the sum of the cubes of its digits is equal to the number itself. 
Where is Java mostly used?
Today developers use Java to construct applications, data centers, game consoles, scientific supercomputers, cell phones, and other devices. In today’s world, Java is the third most popular programming language, after Python and C according to the TIOBE index
We tried to discuss if the given number is an Armstrong number in java or not. In this article. We hope this article gives you a better understanding of Armstrong numbers in java. PrepBytes also provides a good collection of Foundation Courses that can help you enhance your coding skills. Want to make sure you ace the interview in one go? Join our Placement Program that will help you get prepared and land your dream job at MNCs. Mentors of Prepbytes are highly experienced and can provide you with basic, indepth subject knowledge for better understanding.