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 − 371 is an Armstrong number.
371 = 27 + 343 + 1
Let's say we want the Armstrong Numbers between two numbers. 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 .
import java.util.Scanner; public class ArmstrongNumbers { public static void main(String args[]){ int input_1, input_2, check, rem, 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 :"); input_1 = my_scanner.nextInt(); System.out.println("Enter the limit :"); input_2 = my_scanner.nextInt(); System.out.println("The Armstorm numbers are :"); for (i = input_1; i<input_2; i++){ sum = 0; check = i; while(check != 0) { rem = check % 10; sum = sum + (rem * rem * rem); check = check / 10; } if(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 Armstorm 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 input_1, input_2, check, rem, sum, i; input_1 = 1; input_2 = 500; System.out.printf("The first number is %d and the limit is %d ", input_1, input_2); System.out.println("\nThe Armstorm numbers are :"); for (i = input_1; i<input_2; i++){ sum = 0; check = i; while(check != 0) { rem = check % 10; sum = sum + (rem * rem * rem); check = check / 10; } if(sum == i){ System.out.println(i); } } } }
Output
The first number is 1 and the limit is 500 The Armstorm numbers are : 1 153 370 371 407