Java Program to Display Armstrong Number Between Two Intervals



In this article, we will understand how to display the Armstrong numbers between the given two numbers in Java. An Armstrong number is a number that is equal to the sum of the cubes of its own digits.

An integer is called an Armstrong number of order n if it's every digit separate out and cubed and summed up then the sum will be same as the number i.e. abcd... = a3 + b3 + c3 + d3 + ...

In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 is an Armstrong number.

153 = 13 + 53 + 33

For example: 370 is an Armstrong number.

370 = 27 + 343 + 0

Below is a demonstration of the same −

Input

Suppose our input is −

1 & 500

Output

The desired output would be −

The Armstrong numbers between 1 and 500 are 1, 153, 370, 371, 407

Algorithm

Step1 - Start
Step 2 - Declare four integers: my_input_1, my_input_2, i and sum
Step 3 - Prompt the user to enter two integer value/ define the integers
Step 4 - Read the values
Step 5 - Run a for loop to generate Armstrong numbers using %, / and * operator
Step 6 - Divide by 10 and get remainder for ‘check’ .
Step 7 - Multiply ‘rem’ thrice, and add to ‘sum’, and make that the current ‘sum’.
Step 8 - Divide ‘check’ by 10, and make that the current ‘check’.
Step 9 - Display the result
Step 10 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class ArmstrongNumbers {
   public static void main(String args[]){
      int my_low, my_high, check, my_rem, my_sum, i;
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("Required packages have been imported");
      System.out.println("A scanner object has been defined ");
      System.out.println("Enter the first number :");
      my_low = my_scanner.nextInt();
      System.out.println("Enter the limit :");
      my_high = my_scanner.nextInt();
      System.out.println("The Armstrong numbers are :");
      for (i = my_low; i<my_high; i++){
         my_sum = 0;
         check = i;
         while(check != 0) {
            my_rem = check % 10;
            my_sum = my_sum + (my_rem * my_rem * my_rem);
            check = check / 10;
         }
         if(my_sum == i){
            System.out.println(i);
         }
      }
   }
}

Output

Required packages have been imported
A scanner object has been defined
Enter the first number :
1
Enter the limit :
500
The Armstrong numbers are :
1
153
370
371
407

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class ArmstrongNumbers {
   public static void main(String args[]){
      int my_low, my_high, check, my_rem, my_sum, i;
      my_low = 1;
      my_high = 500;
      System.out.printf("The first number is %d and the limit is %d ", my_low, my_high);
      System.out.println("\nThe Armstrong numbers are :");
      for (i = my_low; i<my_high; i++){
         my_sum = 0;
         check = i;
         while(check != 0) {
            my_rem = check % 10;
            my_sum = my_sum + (my_rem * my_rem * my_rem);
            check = check / 10;
         }
         if(my_sum == i){
            System.out.println(i);
         }
      }
   }
}

Output

The first number is 1 and the limit is 500
The Armstrong numbers are :
1
153
370
371
407

Advertisements