Java Program to Display Armstrong Numbers Between Intervals Using Function



In this article, we will understand how to display Armstrong numbers between intervals using function. 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: 371 is an Armstrong number.

371 = 27 + 343 + 1

Below is a demonstration of the same −

Input

Suppose our input is −

The two inputs : 1 and 500

Output

The desired output would be −

The Armstrong numbers are:
153 370 371 407

Algorithm

Step 1 - START
Step 2 - Declare three integer values namely my_low, my_high and i
Step 3 - Read the required values from the user/ define the values
Step 4 - Define a function IsArmstrong which takes an integer value returns Boolean value.
Step 5- In this function, Divide the input variable by 10 and get remainder for ‘check’ .
Step 6 - Then Multiply ‘my_rem thrice, and add to ‘my_sum’, and make that the current ‘my_sum.
Step 7 - Later ‘check’ by 10, and make that the current ‘check’. Compare the ‘my_sum’ with the function input ‘I’ and return true or false.
Step 8 - Using a for loop, iterate from my_low to my_high, for each number, call the function IsArmstrong. If true is returned , it is an Armstrong number, store the number
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, 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 + 1; i < my_high; ++i) {
         if (IsArmstrong (i))
            System.out.print(i + " ");
      }
   }
   public static boolean IsArmstrong(int i) {
      int check, my_rem, my_sum;
      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){
         return true;
      }
      return false;
   }
}

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 :
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, i;
      my_low = 1;
      my_high = 500;
      System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
      System.out.println("The Armstrong numbers are :");
      for(i = my_low + 1; i < my_high; ++i) {
         if (IsArmstrong (i))
            System.out.print(i + " ");
      }
   }
   public static boolean IsArmstrong (int i) {
      int check, my_rem, my_sum;
      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){
         return true;
      }
      return false;
   }
}

Output

The starting and ending numbers are defined as 1 and 500
The Armstrong numbers are :
153 0 371 407

Advertisements