Java Interview Program - Check Whether a Number is Armstrong

Check Whether a Number is Armstrong

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

For example, 153 is an Armstrong number because:

1³ + 5³ + 3³ = 1 + 125 + 27 = 153

This program is a great way to understand number manipulation, loops, and math operations in Java.

Examples

  • Input: 153 → Output: Armstrong number (1³ + 5³ + 3³ = 153)
  • Input: 9474 → Output: Armstrong number (9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474)
  • Input: 123 → Output: Not an Armstrong number
  • Edge Case: 0 → Armstrong number (0ⁱ = 0)

Interviewer Expectations

Interviewers typically ask this question to test your understanding of:

  • Loop constructs
  • Modulus and division operations
  • Math functions like Math.pow()
  • Handling of numeric input and intermediate variables

Approach

Here’s how we solve it step by step:

  1. Take an input number (say, num).
  2. Count the number of digits.
  3. Extract each digit using modulus (%) and divide (/) operations.
  4. Raise each digit to the power of the number of digits and sum them.
  5. If the sum equals the original number, it’s an Armstrong number.

Dry Run for 153

Digits: 1, 5, 3 → Number of digits = 3

1³ = 1, 5³ = 125, 3³ = 27 → Total = 153 → Match!

Java Program

public class ArmstrongNumber {
  public static void main(String[] args) {
    int number = 153;
    int originalNumber = number;
    int temp = number;
    int digits = 0;

    // Count the number of digits
    while (temp != 0) {
      temp /= 10;
      digits++;
    }

    temp = number;
    int result = 0;

    // Calculate the sum of digits raised to the power of 'digits'
    while (temp != 0) {
      int digit = temp % 10;
      result += Math.pow(digit, digits);
      temp /= 10;
    }

    // Check if it is an Armstrong number
    if (result == originalNumber) {
      System.out.println(originalNumber + " is an Armstrong number.");
    } else {
      System.out.println(originalNumber + " is not an Armstrong number.");
    }
  }
}
153 is an Armstrong number.

Possible Followup Questions with Answers

Q1: What is the time complexity of this solution?

Answer: O(n), where n is the number of digits in the number. We process each digit once.

Q2: Can we use this for 4 or 5-digit numbers?

Answer: Yes. The logic works for any number of digits. Just make sure the variable types (like int) can hold the values.

Q3: Can this be implemented using recursion?

Answer: Yes. You can write a recursive method to process digits, though it’s less common in interviews for such problems.

Q4: Can we use String to simplify digit extraction?

Answer: Yes. You can convert the number to a string and loop through its characters. That’s often easier for beginners:

String numStr = "153";
int digits = numStr.length();
int result = 0;
for (char c : numStr.toCharArray()) {
  int digit = c - '0';
  result += Math.pow(digit, digits);
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...