Check Whether a Number Is a Disarium Number
Decide whether a number is Disarium by summing each digit raised to its position.
A Disarium number equals the sum of its digits each raised to the power of its position from the left. Count the digits first, then walk them adding digit^position. For 89 the sum is 8^1 + 9^2 = 8 + 81 = 89, so 89 is a Disarium number.
Problem Statement
A Disarium number is a number that equals the sum of its digits, where each
digit is raised to the power of its position counted from the left (starting at
1). Given n, decide whether it is Disarium and return true or false.
For example, 89 has digits 8 and 9 at positions 1 and 2, giving
8^1 + 9^2 = 8 + 81 = 89. Since that equals the number, 89 is Disarium.
Input: A single positive integer n.
Output: A boolean: true if n is a Disarium number, otherwise false.
Examples
Input: 89
Output: true8^1 + 9^2 = 8 + 81 = 89, which equals the number.
Input: 100
Output: false1^1 + 0^2 + 0^3 = 1, which is not 100.
Constraints
n >= 1 and fits in a 32-bit intPositions are counted from the left starting at 1
Think Before You Code
Reveal the questions to ask yourself first
- The rightmost digit has the highest position, not the lowest — how do you track that?
- How can you learn the total digit count before the main loop?
- What power function raises a digit to its position cleanly?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Work from the right but assign positions from the left.
- Count the number of digits
dinn. - Start
position = dandsum = 0. - While the working copy is not 0:
digit = copy % 10— the current rightmost digit.sum += digit^position.- decrement
positionand drop the digit withcopy /= 10.
- Return
sum == n.
Because the rightmost digit sits at the highest position, position starts at
d and counts down as you move left.
Dry Run
Walk through the example step by step
Checking n = 89 (digit count = 2):
step | copy | digit | position | term | sum
-----+------+-------+----------+-------------+-----
1 | 89 | 9 | 2 | 9^2 = 81 | 81
2 | 8 | 8 | 1 | 8^1 = 8 | 89
end | 0 | — | — | — | 89
sum (89) == n (89) -> true
Solution
Reveal the full Java solution
public class DisariumNumber {
public static boolean isDisarium(int n) {
int digits = countDigits(n);
int copy = n;
int position = digits;
int sum = 0;
while (copy != 0) {
int digit = copy % 10;
sum += (int) Math.pow(digit, position);
position--;
copy /= 10;
}
return sum == n;
}
private static int countDigits(int n) {
if (n == 0) return 1;
int count = 0;
int num = Math.abs(n);
while (num != 0) {
count++;
num /= 10;
}
return count;
}
public static void main(String[] args) {
System.out.println(isDisarium(89)); // true
System.out.println(isDisarium(100)); // false
}
}
The subtle part is the position: even though digits are extracted from the
right, the leftmost digit carries position 1 and the rightmost carries the
highest position. Counting the digits up front lets you start position at
that maximum and decrement it as you move left, so each digit gets the correct
exponent.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Using position values that grow from the right instead of the left.
- Forgetting to count the digits first, so the starting position is wrong.
Edge Cases to Test
- Every single-digit number 1-9 is Disarium because digit^1 equals itself.
- 89 and 175 are the classic multi-digit Disarium numbers to test against.
Interview Follow-Ups
- How would you avoid Math.pow and multiply the power out with an integer loop?
- How does a Disarium number differ from an Armstrong number?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →