Armstrong number generator This is my code to generate all possible Armstrong numbers between the two given numbers. The logic uses string instead of integer to separate the digits to optimize the code.

import java.util.Scanner;

public class ArmstrongNumberGenerator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int firstNumber;
        int lastNumber;
        int sum = 0;
        try {
            System.out.println("\nYou will have to enter initial and final number between which all the armstrong numbers you want to generate\n");
            System.out.println("\nEnter the initial number\n");
            firstNumber = scanner.nextInt();
            System.out.println("\nEnter the final number\n");
            lastNumber = scanner.nextInt();
            if (firstNumber == lastNumber) {
                System.out.println("both initian and final numbers are same , no range to generate armstrong numbers");
            } else {
                if (firstNumber &gt; lastNumber) {
                    System.out.println("initial number is greater than final number so i will alter them and make a range from " + lastNumber + " to " + firstNumber);
                    int temp = firstNumber;
                    firstNumber = lastNumber;
                    lastNumber = temp;
                }
                do {
                    String s = Integer.toString(firstNumber);
                    char[] c = s.toCharArray();
                    for (int i = 0; i &lt; s.length(); i++) {
                        sum = ( int ) (sum + Math.pow((c[i] - 48), c.length));
                    }

                    if (sum == firstNumber) {
                        System.out.println("Number " + firstNumber + " is Armstrong");
                    }
                    ++firstNumber;

                    sum = 0;
                } while (firstNumber &lt; lastNumber);

            }
        } catch (Exception e) {
            System.out.println("invalid data");
        }

    }
}
The first number is included in the range but the last number is not. That inconsistency is odd. You should document the limitations you set to the input. If your limitations make documentation hard, it's a sign of bad programming.

Knowing what I wrote above, right now you to check for both equality and greater than between firstNumber and lastNumber. Just check if (firstNumber &gt; lasNumber) instead and tell the user that "firstNumber must be smaller than lastNumber."

FirstNumber and lastNumber are not descriptive variable names. LowerLimit and upperLimit would be better.

You're not prepared for negative input.

Using firstNumber as both the lower limit and loop counter makes the variable name to be incorrect in both uses. It's really never the lowerLimit nor the number being checked. Add a separate variable and use a loop for (int candidate = lowerLimit; candidate &lt;= upperLimit; candidate++) { ...

You should separate the algorithm from main method that reads the input to a static utility method that operates on integers. Reading code that is nested four deep is difficult.