Check Disarium Number in Java

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit sums

What you’ll learn

  • The precise Disarium rule: digit powers use 1-based positions from the left.
  • How to implement the sum with an LSD-first peel and a running position index.
  • Integer exponentiation (no floating-point Math.pow), a 1-100 scan, live preview, and edge cases like zero.

Overview

A Disarium number equals the sum of its decimal digits, each raised to the power of its position from the left (most significant digit is position 1). The classic example is 89 = 81 + 92.

Two programs

Single test on 89 and a range sweep 1-100 matching the reference output.

Live preview

Try small positives in the browser with the same sum rule.

Fixes vs reference

countDigits(0) and integer ipow instead of floating-point power functions.

Prerequisites

Digit extraction with % 10, integer division, and loops in Java.

What is a Disarium number?

Write the decimal digits of n as d1d2...dk from left to right. Then n is Disarium if n = d1^1 + d2^2 + ... + dk^k.

This is not the same as Armstrong (narcissistic) numbers, which use a fixed exponent equal to the digit count for every digit.

Formal sum

For digits d1...dk from left to right, n is Disarium iff n = d1^1 + d2^2 + ... + dk^k.

Intuition and examples

89 = 8^1 + 9^2 works, while 10 = 1^1 + 0^2 = 1 does not.

Live preview

Live result
Press "Check" to evaluate.

📜 Pseudocode

Pseudocode
function isDisarium(n):  // n > 0
    k <- countDigits(n)
    sum <- 0
    pos <- k
    work <- n
    while work > 0:
        digit <- work mod 10
        sum <- sum + digit^pos
        pos <- pos - 1
        work <- floor(work / 10)
    return sum = n
1

Single value: 89

java
public class Main {

    static int ipow(int base, int exp) {
        int r = 1;
        for (int i = 0; i < exp; i++) {
            r *= base;
        }
        return r;
    }

    static int countDigits(int number) {
        if (number == 0) {
            return 1;
        }
        int count = 0;
        int n = number;
        while (n != 0) {
            count++;
            n /= 10;
        }
        return count;
    }

    static boolean isDisarium(int number) {
        if (number <= 0) {
            return false;
        }
        int originalNumber = number;
        int digitCount = countDigits(number);
        int sum = 0;

        while (number != 0) {
            int digit = number % 10;
            sum += ipow(digit, digitCount);
            digitCount--;
            number /= 10;
        }

        return sum == originalNumber;
    }

    public static void main(String[] args) {
        int inputNumber = 89;

        if (isDisarium(inputNumber)) {
            System.out.println(inputNumber + " is a Disarium number.");
        } else {
            System.out.println(inputNumber + " is not a Disarium number.");
        }
    }
}
2

Disarium numbers from 1 to 100

java
public class Main {

    static int ipow(int base, int exp) {
        int r = 1;
        for (int i = 0; i < exp; i++) {
            r *= base;
        }
        return r;
    }

    static int countDigits(int num) {
        if (num == 0) {
            return 1;
        }
        int count = 0;
        int n = num;
        while (n != 0) {
            n /= 10;
            count++;
        }
        return count;
    }

    static boolean isDisarium(int num) {
        if (num <= 0) {
            return false;
        }
        int originalNum = num;
        int digitCount = countDigits(num);
        int sum = 0;

        while (num != 0) {
            int digit = num % 10;
            sum += ipow(digit, digitCount);
            num /= 10;
            digitCount--;
        }

        return sum == originalNum;
    }

    public static void main(String[] args) {
        System.out.println("Disarium numbers in the range 1 to 100:");

        for (int i = 1; i <= 100; i++) {
            if (isDisarium(i)) {
                System.out.print(i + " ");
            }
        }

        System.out.println();
    }
}

Optimization

Precompute powers for digits 0..9 by position when scanning large ranges.

❓ FAQ

A positive integer n is Disarium if the sum of its decimal digits, each raised to the power of its 1-based position counting from the left (most significant digit has position 1), equals n. Example: 89 = 8^1 + 9^2.
The code peels digits from the right with % 10, but the rightmost digit has the largest position index (units are the last position). Decrementing digitCount after each peel assigns the correct exponent.
Math.pow returns double. An integer ipow helper avoids floating-point conversion and keeps the logic clear and exact for small integer exponents.
countDigits should treat 0 as having one digit for consistency, but most Disarium definitions focus on positive integers. This page follows that common convention and checks n > 0.
For positive digits 1–9, the sum is d^1 = d, so yes. Zero is usually excluded from the definition in common lists.
O(d) digits per test, with each ipow up to O(d) multiplications in a simple implementation. For fixed-width ints, d is small.

🔄 Input / output examples

89 -> Disarium, 135 -> Disarium, 10 -> not Disarium.

Edge cases

Zero and negatives are excluded by this tutorial's positive-number convention.

⏱️ Time and space complexity

Per number complexity is linear in digit count with constant extra space.

Summary

  • Disarium uses positional powers from left to right.
  • Java implementation peels digits and tracks exponent index.
Did you know?

Besides 89, 135 is a classic Disarium example: 11 + 32 + 53 = 1 + 9 + 125 = 135. Every one-digit positive integer 1-9 satisfies the rule because d1 = d.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful