- 1Java Interview Program Title
- 2Java Interview Program - Check Whether a Number is Armstrong
- 3Java Interview Program - Check Whether a Number is an Ugly Number
- 4Java Interview Program - Check Whether a Number is a Sunny Number in Java
- 5Java Interview Program - Check Whether a Number is a Harshad (Niven) Number
- 6Check whether a number is a Hamming number in Java
- 7Check Whether a Number is a Spy Number in Java
- 8Check whether a Number is a Smith Number in Java
- 9Check Whether a Number is a Disarium Number in Java
- 10Check Whether a Number is a Lychrel Number Candidate in Java
- 11Check Whether a Number is a Tech Number in Java
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:
- Take an input number (say,
num
). - Count the number of digits.
- Extract each digit using modulus (%) and divide (/) operations.
- Raise each digit to the power of the number of digits and sum them.
- 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);
}
Next Topic ⮕Java Interview Program - Check Whether a Number is an Ugly Number
Comments
Loading comments...