I have written this program but can't find the error in this to get desired result.
I have given the range of the number by user input and want to find armstrong numbers between them.
Input: Enter the range to print armstrong number between them. 100 10000
Expected Output: Armstrong Number between 100 and 10000:
153 370 371 407 1634 8208 9474
My Program Output: Enter the Range to Print Armstrong Number between Them
100
10000
There is no Armstrong Number between 100 and 10000
import java.util.Scanner;
public class ArmstrongList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the Range to Print Armstrong Number between Them");
int start = scanner.nextInt();
int end = scanner.nextInt();
int cubeSum = 0, digit, counter = 0;
for(int i = start; i < end; i++){
int count = 0;
int num = i;
while(num!=0) { //Count no. of digits in the Number
num /= 10;
++count;
}
int temp = i;
while(temp!=0) {
digit = temp % 10;
cubeSum = cubeSum + (int)(Math.pow(digit , count));
temp/=10;
}
if(cubeSum == i) {
System.out.println(i + " is an Armstrong number ");
if(counter == 0){
System.out.println("Armstrong Number between " + start + " and " + end + ": ");
}
System.out.println(i + " ");
counter++;
}
}
if(counter == 0) {
System.out.println("There is no Armstrong Number between " + start + " and " + end);
}
}
}