Java ProgramsControl FlowCheck Number Disarium

Check Number Disarium in Java

intermediate·  Control Flow  ·  Loops

Problem

A Disarium number is a number where each digit raised to the power of its position (counted from the left, starting at 1) sums back up to the number itself.

Given a number, determine whether it is a Disarium number.

Input
135
Output
135 is a Disarium number: true

Java Program

Java
public class DisariumNumberCheck { public static void main(String[] args) { int n = 135; String digits = String.valueOf(n); long sum = 0; for (int i = 0; i < digits.length(); i++) { int digit = digits.charAt(i) - '0'; int position = i + 1; sum += Math.round(Math.pow(digit, position)); // guards against floating-point drift } System.out.println(n + " is a Disarium number: " + (sum == n)); } }

Output

135 is a Disarium number: true

Core Logic

Converting the number to a string exposes both its digit count and each digit's left-to-right position, which is exactly what raising each digit to its own position needs.

How It Works
  1. 1String.valueOf(n) gives access to each digit character and its index.
  2. 2The loop runs i from 0 to the string's length, treating i + 1 as that digit's 1-indexed position.
  3. 3Each digit is raised to the power of its position with Math.pow(), rounded to the nearest whole number, and added into sum.
  4. 4Once every digit has been processed, sum is compared against the original n.
For 135: 1^1 = 1, 3^2 = 9, and 5^3 = 125 — summing to 135, which matches the original number.
💡

Key Point: Math.round() matters here — Math.pow() returns a double, and its floating-point result can drift slightly away from the exact integer value it's meant to represent.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The loop computes one power per digit, and Math.pow()'s own cost doesn't grow with how large n itself is.

Key Concepts

String conversionMath.pow()digit position

Approach 2: Java 8

Java
import java.util.stream.IntStream; public class DisariumNumberCheckStream { public static void main(String[] args) { int n = 135; String digits = String.valueOf(n); // Streams the index range so each digit's position stays available inside the mapping step long sum = IntStream.range(0, digits.length()) .mapToLong(i -> Math.round(Math.pow(digits.charAt(i) - '0', i + 1))) .sum(); System.out.println(n + " is a Disarium number: " + (sum == n)); } }

Output

135 is a Disarium number: true

Core Logic

Streaming the digit string's indices, instead of looping over them by hand, lets each index double as both the digit's position and the lookup into the string.

How It Works
  1. 1String.valueOf(n) gives the digit string, same as the manual version.
  2. 2IntStream.range(0, digits.length()) produces every valid index into that string.
  3. 3.mapToLong(i -> Math.round(Math.pow(digits.charAt(i) - '0', i + 1))) reads the digit at index i, raises it to the power of its 1-indexed position, and rounds away floating-point drift, all inside the mapping step.
  4. 4.sum() totals every mapped power into the final sum.
For 135, the index stream maps to 1^1 = 1, 3^2 = 9, and 5^3 = 125, summing to 135.
💡

Key Point: Streaming the index range, rather than the characters directly, is what keeps each digit's position available inside the lambda — a plain chars() stream would lose track of where each digit sits.

Complexity
Time Complexity: O(d)Space Complexity: O(1)

Why: The stream computes one power per digit just like the loop, generating indices on demand without extra storage.

Key Concepts

StreamIntStream.range()mapToLong()sum()

Related Programs