-1

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);
    }
        
    }
}

1 Answers1

0

The problem is that you don't reset cubeSum to zero on each iteration.

There is no benefit in declaring variables outside the loop. Variables don't actually cost anything to declare inside a loop(*): they don't even exist at runtime. At runtime, it is just "some place in memory" where a value is stored and read from. So, declare them in the tightest scope possible, in order to avoid accidentally reusing value from previous computations.

Move the declaration of the cubeSum into the for loop, ideally immediately before the while loop where it is incremented (while you're at it, declare digit inside the loop too):

    int cubeSum = 0;
    while(temp!=0) {
        int digit = temp % 10;
        cubeSum = cubeSum + (int)(Math.pow(digit , count));
        temp/=10;
    }

Ideone demo

Learn to use a debugger

An even better approach could be to declare methods to calculate the cube sum (and count, similarly):

int cubeSum(int i, int count) {
  int cubeSum = 0;
  while(i!=0) {
    int digit = i % 10;
    cubeSum = cubeSum + (int)(Math.pow(digit , count));
    i/=10;
  }
  return cubeSubm;
}

This scopes the variables even more tightly, so you don't see the intermediate computations, and get a much clearer main method, with effectively final variables:

int count = count(num);
int cubeSum = cubeSum(i, count);
if (cubeSum == i) {
  // Print that it's an Armstrong number.
}

(*) The value assigned to a variable might cost something, if you create a new Whatever() on each iteration, or evaluate some expensive function; but assigning a constant primitive has almost zero cost.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243