Print armstrong numbers in the given range

Armstrong number in java

A number is 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 three-digit Armstrong numbers and four-digit Armstrong numbers too.

Let’s look at 371 as an example to understand why it’s an Armstrong number.

371 = 3*3*3 + 7*7*7 + 1*1*1

    = 27 + 343 +  1

    = 371

Formula for calculating Armstrong number

wxyz = pow(w,n) + pow(x,n) + pow(y,n) + pow(z,n)

Let’s see more that how we can check Armstrong number in java……..

To print Armstrong numbers between the given range in java, we first need to understand what is an Armstrong number. An Armstrong number is the number that is equal to the sum of the cubes of the single individual digits of the number. For example, we want to check if 153 is an Armstrong number or not. We will take the sum of cubes of 1, 5 and 3 which is 1+125+27. The sum is equal to 153. As a result, we conclude that 153 is an Armstrong number. The program in java to print an Armstrong number is below.

Algorithm for Armstrong Number in Java :

  • Take an integer variable x
  • Value is assigned to the variable x
  • The digits of the value are split
  • The cube value of each digit is found
  • Add the values of all the cubes
  • The output is saved to sum variable
  • If sum=X, print Armstrong number
  • If sum != X, print not Armstrong number

Program for Armstrong number in Java

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 +=digit*digit*digit;
            num /= 10;

        if(cubeSum == originalNum)
            System.out.println(originalNum+ " is an Armstrong number");
            System.out.println(originalNum+ " is not an Armstrong number");

Dry Run for Armstrong Number in Java

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
for loop condition check: num is not equal to zero loops will run again
for i=2
for loop condition: num is not equal to zero loops will run again
for i=3
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

Output :

Enter the Number : 145
145 is not an Armstrong number

Program for Armstrong Number in Java using For Loop

import java.util.Scanner;
class Armstrong
    public static void main(String[] arg)
    int a,arm=0,n,temp;
    Scanner sc=new Scanner(;
    System.out.println("Enter a number");
    for( ;n!=0;n/=10 )
    System.out.println(temp+" is a Armstrong number ");
    System.out.println(temp+" is not a Armstrong number ");

Output :

Enter the Number: 155
153 is a Armstrong number

Armstrong number between the given ranges

import java.util.Scanner;
public class DeveloperHelps
public static void main(String args[])
int n, n1, n2, i, rem, temp, count=0;
Scanner scan = new Scanner(;
System.out.print("Enter the starting number of the range :  ");
n1 = scan.nextInt();
System.out.print("Enter the ending number of the range :  ");
n2 = scan.nextInt();		       
for(i=n1+1; i<n2; i++) {
temp = i;
n = 0;
while(temp != 0)
rem = temp%10;
n = n + rem*rem*rem;
temp = temp/10;
if(i == n)
if(count == 0)
System.out.print("Armstrong Numbers between the given range are: \n");
System.out.print(i + "  ");
if(count == 0){
System.out.print("Armstrong Number is not Found between the given range");

The output of the following program will be:

Enter the starting number of the range:  90
Enter the starting number of the range:  501
Armstrong Numbers between the given range are: 153 370 371 407

The user enters the range in which he wants to find the Armstrong numbers in the above program. As the user enters the starting and ending range, a ‘for’ loop is used which finds all the Armstrong numbers. We use our logic for checking the numbers. If we find a number that lies in the Armstrong number category, the value is printed in the output. Otherwise, a message is displayed that Armstrong Number is not found between the given range.


Time Complexity: O(log(n))
O(log(n)) + O(log(n)) = O(log(n))
The above program for checking whether a number is Armstrong or not has a time complexity of O(log(n)) as both the while loop runs for log(n) times because at each iteration the number is divided by 10, where n is the number given as input.

Space Complexity: O(1)
space complexity of the armstrong program is O(1) as no extra variable is taken to store the value. All the variables initialized takes constant O(1) space.

Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.

Leave a comment

Your email address will not be published. Required fields are marked *