Java program to print armstrong number from 1 to 1000

Let’s learn java program to print armstrong number from 1 to 1000.

java program to print armstrong number from 1 to 1000

Java program to print armstrong number from 1 to 1000

Armstrong number in java is one of the common java interview question.

Also read – armstrong number program in java using scanner

An armstrong number is sum of cube of its own digits. Here modulus and division operator is used alongside “for” loops and “if” condition.

Here’s the java program for armstrong number between 1 to 1000,

public class ArmstrongNumberDemo
{
   public static void main(String[] args)
   {
      int number, temp, total = 0;
      System.out.print("Armstrong number between 1 to 1000 : ");
      for(int a = 1; a <= 1000; a++)
      {
         number = a;
         while(number > 0)
         {
            temp = number % 10;
            total = total + (temp * temp * temp);
            number = number / 10;
         }
         if(total == a)
         {
            System.out.print(a + " ");
         }
         total = 0;
      }
   }
}



Output:

Armstrong number between 1 to 1000 : 1 153 370 371 407


Java program to print all armstrong numbers between given range

In java program to print all armstrong numbers between given range user is inputs given range, that is, starting number and ending number.

import java.util.Scanner;

public class ArmstrongNumbersGivenRange 
{
   public static void main(String[] args) 
   {
      int number, startNumber, endNumber, a, rem, temp, count = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter starting number between given range: ");
      startNumber = sc.nextInt();
      System.out.print("Please enter ending number between given range: ");
      endNumber = sc.nextInt();
      for(a = startNumber + 1; a < endNumber; a++)
      {
         temp = a;
         number = 0;
         while(temp != 0)
         {
            rem = temp % 10;
            number = number + rem * rem * rem;
            temp = temp / 10;
         }
         if(a == number)
         {
            if(count == 0)
            {
               System.out.print("Armstrong numbers between given range " + startNumber + " and " + endNumber + ": ");
            }
            System.out.print(a + "  ");
            count++;
         }
      }
      // if there is no Armstrong number found between range
      if(count == 0)
      {
         System.out.print("Sorry!! There's no armstrong number between given range " + startNumber + " and " + endNumber);
      }
      sc.close();
   }
}


Output:

Please enter starting number between given range: 100
Please enter ending number between given range: 600
Armstrong numbers between given range 100 and 600: 153 370 371 407

Also read – armstrong number in java using recursion

Please enter starting number between given range: 1
Please enter ending number between given range: 100
Sorry!! There’s no armstrong number between given range 1 and 100

Also read – 4 digit armstrong number in java