Table of Contents
In this blog, we will discuss what is Armstrong number and also create Java programs to check if the given number is an Armstrong number or not. The Armstrong number program are frequently asked in Java coding interviews and academics.
Armstrong Number
AnĀ ArmstrongĀ number is a positive m-digit number that is equal to the sum of the mthĀ powers of their digits. It is also known asĀ pluperfect, orĀ Plus Perfect, orĀ NarcissisticĀ number. It is an OEIS sequenceĀ A005188. Letās understand it through an example.
Armstrong Number Example
1:Ā 11Ā =Ā 1
2:Ā 21Ā =Ā 2
3:Ā 31Ā =Ā 3
153:Ā 13Ā + 53Ā + 33Ā = 1 + 125+ 27 =Ā 153
125:Ā 13Ā + 23Ā + 53Ā = 1 + 8 + 125 =Ā 134 (Not an Armstrong Number)
1634:Ā 14Ā + 64Ā + 34Ā + 44Ā = 1 + 1296 + 81 + 256 =Ā 1643
Similarly, we can check other number also.
The first few Armstrong numbers between 0 to 999 areĀ 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407.Ā Some other Armstrong numbers are 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050, 24678051, 88593477, 146511208, 472335975, 534494836, 912985153, 4679307774, 32164049650, 32164049651.
Note that there is no two-digit Armstrong number.
Grab the opportunity to learn Python with Entri! Click Here
Armstrong Program in JavaĀ
1: What is the default value of a boolean in Java?
The following Java program prints all the Armstrong numbers up to the specified limit.
ArmstrongNumberExample1.java
- importĀ java.util.Scanner;
- importĀ java.lang.Math;
- publicĀ classĀ ArmstsrongNumberExample
- {
- //functionĀ toĀ checkĀ ifĀ theĀ numberĀ isĀ ArmstrongĀ orĀ not
- staticĀ booleanĀ isArmstrong(intĀ n)
- {
- intĀ temp,Ā digits=0,Ā last=0,Ā sum=0;
- //assigningĀ nĀ intoĀ aĀ tempĀ variable
- temp=n;
- //loopĀ executeĀ untilĀ theĀ conditionĀ becomesĀ false
- while(temp>0)
- {
- tempĀ =Ā temp/10;
- digits++;
- }
- tempĀ =Ā n;
- while(temp>0)
- {
- //determinesĀ theĀ lastĀ digitĀ fromĀ theĀ numberĀ Ā Ā Ā
- lastĀ =Ā tempĀ %Ā 10;
- //calculatesĀ theĀ powerĀ ofĀ aĀ numberĀ upĀ toĀ digitĀ timesĀ andĀ addĀ theĀ resultantĀ toĀ theĀ sumĀ variable
- sumĀ +=Ā Ā (Math.pow(last,Ā digits));
- //removesĀ theĀ lastĀ digitĀ
- tempĀ =Ā temp/10;
- }
- //comparesĀ theĀ sumĀ withĀ n
- if(n==sum)
- //returnsĀ ifĀ sumĀ andĀ nĀ areĀ equal
- returnĀ true;
- //returnsĀ falseĀ ifĀ sumĀ andĀ nĀ areĀ notĀ equal
- elseĀ returnĀ false;
- }
- //driverĀ code
- publicĀ staticĀ voidĀ main(StringĀ args[])
- {
- intĀ num;
- ScannerĀ sc=Ā newĀ Scanner(System.in);
- System.out.print(“EnterĀ theĀ limit:Ā “);
- //readsĀ theĀ limitĀ fromĀ theĀ user
- num=sc.nextInt();
- System.out.println(“ArmstrongĀ NumberĀ upĀ toĀ “+Ā numĀ +Ā ”Ā are:Ā “);
- for(intĀ i=0;Ā i<=num;Ā i++)
- //functionĀ calling
- if(isArmstrong(i))
- //printsĀ theĀ armstrongĀ numbers
- System.out.print(i+Ā “,Ā “);
- }
- }
Output:
Enter the limit: 999 Armstrong Number up to 999 are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
Letās create another Java program that checks if the given number is an Armstrong number or not.
ArmstrongNumberExample2.java
- importĀ java.util.Scanner;
- importĀ java.lang.Math;
- publicĀ classĀ ArmstsrongNumberExample2
- {
- //functionĀ toĀ checkĀ ifĀ theĀ numberĀ isĀ ArmstrongĀ orĀ not
- staticĀ booleanĀ isArmstrong(intĀ n)
- {
- intĀ temp,Ā digits=0,Ā last=0,Ā sum=0;
- //assigningĀ nĀ intoĀ aĀ tempĀ variable
- temp=n;
- //loopĀ executeĀ untilĀ theĀ conditionĀ becomesĀ false
- while(temp>0)
- {
- tempĀ =Ā temp/10;
- digits++;
- }
- tempĀ =Ā n;
- while(temp>0)
- {
- //determinesĀ theĀ lastĀ digitĀ fromĀ theĀ numberĀ Ā Ā Ā
- lastĀ =Ā tempĀ %Ā 10;
- //calculatesĀ theĀ powerĀ ofĀ aĀ numberĀ upĀ toĀ digitĀ timesĀ andĀ addĀ theĀ resultantĀ toĀ theĀ sumĀ variable
- sumĀ +=Ā Ā (Math.pow(last,Ā digits));
- //removesĀ theĀ lastĀ digitĀ
- tempĀ =Ā temp/10;
- }
- //comparesĀ theĀ sumĀ withĀ n
- if(n==sum)
- //returnsĀ ifĀ sumĀ andĀ nĀ areĀ equal
- returnĀ true;
- //returnsĀ falseĀ ifĀ sumĀ andĀ nĀ areĀ notĀ equal
- elseĀ returnĀ false;
- }
- //driverĀ code
- publicĀ staticĀ voidĀ Ā main(StringĀ args[])
- {
- intĀ num;
- ScannerĀ sc=Ā newĀ Scanner(System.in);
- System.out.print(“EnterĀ theĀ number:Ā “);
- //readsĀ theĀ limitĀ fromĀ theĀ user
- num=sc.nextInt();
- if(isArmstrong(num))
- {
- System.out.print(“ArmstrongĀ “);
- }
- else
- {
- System.out.print(“NotĀ ArmstrongĀ “);
- }
- }
- }
Output 1:
Enter the number: 2 Armstrong
Output 2:
Enter the number: 1675 Not Armstrong
Explore Free Coding Courses !
Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.
š Explore Free Courses NowArmstrong Number in Java
In this blog, we will develop the Armstrong number program in Java. First, we will develop a java program to check an Armstrong number, and then we will develop a java program for an Armstrong number between 1 to 1000.
A positive integer is called Armstrong number of order n if,Ā abcdā¦. =Ā anĀ +Ā bnĀ +Ā cnĀ +Ā dnĀ +Ā ā¦.
For Example :-Ā 153
13Ā +Ā 53Ā +Ā 33Ā = 1 + 125 + 27 = 153
So, 153 is an Armstrong number of order 3.
4150Ā =Ā 45Ā +Ā 15Ā +Ā 55Ā +Ā 05Ā = 1,024 + 1 + 3,125 + 0 = 4150
So, 4150 is an Armstrong number of order 5
Procedure to check Armstrong number of order N
1) Take a variable and take an order to check
2) Declare variablesĀ lastDigit,Ā power, andĀ sumĀ Initialize sum with 0
3) Take a temporary variableĀ nĀ to store numbers
4) Find the last digit ofĀ n
5) Calculate the power of thatĀ lastDigitĀ with order i.e.Ā pow(lastDigit,Ā order)
6) Add the result into theĀ sum
7) Remove the last digit
8) Repeat steps 4 to 7 until the number becomes 0
9) Compare sum value and the actual number
==> If both are the same then it is the Armstrong number of the given order
==> Else it is not Armstrong number of the given order
Java method to check Armstrong number of order N
public static boolean isArmstrong(int number, int order){
// declare variables
int lastDigit = 0;
int power = 0;
int sum = 0;
// temporary variable to store number
int n = number;
while(n!=0) {
// find last digit
lastDigit = n % 10;
// find power of digit
power = (int) Math.pow(lastDigit, order);
// add power value into sum
sum += power;
// remove last digit
n /= 10;
}
if(sum == number) return true;
else return false;
}
In this method to find the power, we take the support of theĀ pow(), which is defined in the Math class. TheĀ pow() is a static method so we can call it without creating an object of the Math class, and it returns double so we need to use type casting it to int type.
Learn to code from industry experts! Enroll here
Java program for Armstrong number between 1 to 1000
We can also find all Armstrong numbers of a given order in the given range. For this purpose, we need to take the minimum, and maximum values of the range and order to check the Armstrong number.
import java.util.Scanner;
public class ArmstrongNumberInRange {
public static boolean isArmstrong(int number, int order){
// declare variables
int lastDigit = 0;
int power = 0;
int sum = 0;
// temporary variable to store number
int n = number;
while(n!=0) {
// find last digit
lastDigit = n % 10;
// find power of digit
power = (int) Math.pow(lastDigit, order);
// add power value into sum
sum += power;
// remove last digit
n /= 10;
}
if(sum == number) return true;
else return false;
}
public static void main(String[] args) {
// declare variables
int minRange , maxRange;
int order = 0;
// create Scanner class object
Scanner scan = new Scanner(System.in);
// read inputs
System.out.print("Enter min & max "+
"Range value:: ");
minRange = scan.nextInt();
maxRange = scan.nextInt();
System.out.print("Enter order to check::");
order = scan.nextInt();
// check in range
System.out.println("Armstrong numbers"+
" from "+minRange+" to "+maxRange+
" of order " +order+" is:: ");
for(int i = minRange; i<= maxRange; i++)
if(isArmstrong(i, order))
System.out.print( i + " ");
// close Scanner class object
scan.close();
}
}
The output for different test-cases :-
Enter min and max Range value: 1 1000
Enter an order to check: 3
Armstrong numbers from 1 to 1000 of order 3 is:
1 152 370 371 407
Enter min and max Range value: 1 10000
Enter an order to check: 5
Armstrong numbers from 1 to 10000 of order 5 is:
1 4150 4151





