In this tutorial you will learn about the Java Program to print Armstrong numbers between a given range and its application with practical example.
In this tutorial, we will learn to create a Java Program to print Armstrong numbers between a given range using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Armstrong number.
A number is said to be an Armstrong number if the given number equals to the sum of the cubes of individual digits said to be Armstrong number
Example, 153 is a Armstrong Number.
153 = (1)3 + (5)3 + (3)3.
=> 153 = 1 + 125 + 27
153 = 153
Java Program to print Armstrong numbers between a given range
In this program we would find the Armstrong number between Given Range .First of all we take range from initial point to final point form user and find the Armstrong number with in a range.let have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//java program to find Armstrong number between range import java.util.Scanner; public class Armstrong_number{ public static void main(String args[]){ // declaring variables and taking value from user at run time int number, starting, ending, i, r, temp, count=0; Scanner scanner = new Scanner(System.in); System.out.print("Enter the starting Range "); starting = scanner.nextInt(); System.out.print("Enter the ending Range "); ending = scanner.nextInt(); scanner.close(); //logic to create Armstrong numbers between start and end for(i=starting+1; i<ending; i++) { temp = i; number = 0; while(temp != 0) { r = temp%10; number = number + r*r*r; temp = temp/10; } if(i == number) { if(count == 0) { System.out.print("Armstrong Numbers Between "+starting+" and "+ending+": "); } System.out.print(i + " "); count++; } } // When no Armstrong number is found if(count == 0) { System.out.print("There is no Armstrong number Between "+starting+" and "+ending); } } } |
Output
Armstrong Number
When No Armstrong Number Found
In the above program, we have first declared and initialized a set variables required in the program.
- Starting = it will hold entered starting range.
- Ending = it wil hold entered last value with in the range.
- i= for itertion.
- temp and r= it will hold temporary values.
- count=it will tell us number is Armstrong number or not.
After declaring variables we take range from user and find Armstrong number between number.
Algorithm
1.Take ranges to find Armstrong
2. Assign values to the variables.
3. Break the digits of Armstrong numbers.
4. Then find the cube–value of Digits.
5. then by adding all cube–values together to check Armstrong number
6. Save the values in variable number.
7. If number is equals to Armstrong number show Armstrong Number
all the process show in below image..
if now Armstrong number found within the range print.