We have already seen java program to check Armstrong number. In this tutorial, we will write a java program to print Armstrong numbers between a given range.
Java Example to Print Armstrong numbers between a given range
In this program user is asked to enter the starting and ending numbers and the program then prints the Armstrong numbers between these input numbers.
package com.beginnersbook; import java.util.Scanner; public class JavaExample { public static void main(String args[]) { int num, start, end, i, rem, temp, counter=0; Scanner scanner = new Scanner(System.in); System.out.print("Enter the start number: "); start = scanner.nextInt(); System.out.print("Enter the end number: "); end = scanner.nextInt(); scanner.close(); //generate Armstrong numbers between start and end for(i=start+1; i<end; i++) { temp = i; num = 0; while(temp != 0) { rem = temp%10; num = num + rem*rem*rem; temp = temp/10; } if(i == num) { if(counter == 0) { System.out.print("Armstrong Numbers Between "+start+" and "+end+": "); } System.out.print(i + " "); counter++; } } // if no Armstrong number is found if(counter == 0) { System.out.print("There is no Armstrong number Between "+start+" and "+end); } } }
Output:
Related Java Examples
1. Java program to print Pascal’s triangle
2. Java program to break integer into digits
3. Java program to print prime numbers between a given range
4. Java program to generate random number
Leave a Reply