- 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
Check Whether a Number is a Disarium Number in Java
Topic: Check Whether a Number is a Disarium Number
A Disarium number is a number in which the sum of its digits powered with their respective positions is equal to the number itself.
For example, in the number 135:
- 11 + 32 + 53 = 1 + 9 + 125 = 135
This concept uses basic arithmetic operations and the use of Math.pow
to raise digits to a power, as well as converting numbers to strings to get digit positions easily.
Examples
Example 1:
Input: 89
Calculation: 81 + 92 = 8 + 81 = 89
Output: Disarium Number
Example 2:
Input: 75
Calculation: 71 + 52 = 7 + 25 = 32
Output: Not a Disarium Number
Example 3 (Edge Case):
Input: 1
Output: Disarium Number
— Since 11 = 1
Interviewer Expectations
The interviewer is checking whether you can:
- Understand digit manipulation in numbers
- Use string operations and indexing
- Apply loops and arithmetic operations effectively
- Write clean and readable code with correct logic
Approach
- Convert the number to a string so that each digit’s position can be easily tracked.
- Initialize a variable to store the sum.
- Loop through each digit and compute digitposition using
Math.pow()
. - Sum all powered digits.
- Compare the sum with the original number.
Dry Run (with 135):
- Digits: 1 (pos 1), 3 (pos 2), 5 (pos 3)
- Sum = 11 + 32 + 53 = 1 + 9 + 125 = 135
- Since the sum equals the original number, it is a Disarium number.
Java Program
public class DisariumNumber {
public static void main(String[] args) {
int num = 135;
String strNum = Integer.toString(num);
int sum = 0;
for (int i = 0; i < strNum.length(); i++) {
int digit = Character.getNumericValue(strNum.charAt(i));
sum += Math.pow(digit, i + 1);
}
if (sum == num) {
System.out.println(num + " is a Disarium Number");
} else {
System.out.println(num + " is Not a Disarium Number");
}
}
}
135 is a Disarium Number
Possible Followup Questions with Answers
1. Can we solve it without converting to String?
Yes. We can count digits first using a loop, then extract digits from right to left and process their position accordingly. However, it’s more complex and less readable.
2. What is the time complexity of this program?
O(n), where n is the number of digits in the number, because we loop once through all digits.
3. Can we generalize this for a list of numbers?
Yes. Just wrap the logic inside a method and call it for multiple values using a loop or from user input.
4. What other numbers fall in the special category?
Some other interesting number types:
- Armstrong Number
- Perfect Number
- Harshad Number
- Automorphic Number
Next Topic ⮕Check Whether a Number is a Lychrel Number Candidate in Java
Comments
Loading comments...