Check Disarium Number in C

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 math.h), 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 pow.

Prerequisites

Decimal digits, % 10 peel, loops, and nonnegative integer conventions.

  • #include <stdio.h>, int main(void) (no math.h in the samples on this page).
  • Optional: long long if you extend to large powers.

What is a Disarium number?

Write the decimal digits of n as d1d2…dk from left to right. Then n is Disarium if n = d11 + d22 + … + dkk.

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

Disarium exponent = position
89 8¹+9²
135 1¹+3²+5³

Formal sum

Let k = ⌊log10 n⌋ + 1 be the number of digits of positive n. Define positions p = 1..k from the left. Disarium means n = ∑p=1k dpp.

89

81 + 92 = 8 + 81 = 89.

Intuition

89 Disarium
Sum
8 + 81
10 Not
Sum
1 + 0 ≠ 10

Takeaway: the rightmost digit gets the largest exponent in the Disarium sum.

Live preview

Positive integers in the JavaScript safe range. Zero and negatives are rejected here.

Shows the digit-power sum and whether it matches n.

Live result
Press “Check” to evaluate.

Algorithm

Goal: decide whether positive n satisfies the Disarium sum.

Count digits k

Treat n = 0 as a special case (definition usually excludes it); otherwise count decimal digits.

Peel from the right

Let pos start at k. While work > 0: add digitpos to sum, decrement pos, divide work by 10.

Compare

Return whether sum equals the original n.

📜 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

Same logic as the reference with ipow for exact integers and countDigits that returns 1 for 0 (only needed if you extend tests).

c
#include <stdio.h>

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

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

int isDisarium(int number) {
    if (number <= 0) {
        return 0;
    }
    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;
}

int main(void) {
    int inputNumber = 89;

    if (isDisarium(inputNumber)) {
        printf("%d is a Disarium number.\n", inputNumber);
    } else {
        printf("%d is not a Disarium number.\n", inputNumber);
    }

    return 0;
}

Explanation

For 89, k = 2. First peeled digit is 9 at position 2, then 8 at position 1.

if (number <= 0) return 0;

Convention. Keeps the sample aligned with positive Disarium numbers.

2

Disarium numbers from 1 to 100

Same output as the reference: single digits plus 89.

c
#include <stdio.h>

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 int isDisarium(int num) {
    if (num <= 0) {
        return 0;
    }
    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;
}

int main(void) {
    printf("Disarium numbers in the range 1 to 100:\n");

    for (int i = 1; i <= 100; ++i) {
        if (isDisarium(i)) {
            printf("%d ", i);
        }
    }

    printf("\n");
    return 0;
}

Explanation

Single-digit numbers satisfy d1 = d. The only two-digit Disarium in this range is 89.

Optimization

Precompute powers. Digits are at most 9; you can cache 9p for p ≤ d when scanning many n.

Prune ranges. Simple bounds on the sum can skip impossible lengths, useful for much larger intervals than 100.

Interview: define positions clearly; mention Armstrong contrast; use integer powers.

❓ 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.
pow returns double; integer exponentiation with small bases is exact in double, but a tiny integer ipow helper avoids dependency and any rounding surprises for interview-style code.
countDigits must treat 0 as having one digit for a consistent loop; otherwise digitCount becomes 0 and the Disarium sum is vacuously 0, which blurs the usual definition focused on positive integers.
For positive digits 1–9, the sum is d^1 = d, so yes. Zero is usually excluded from the definition in lists like OEIS A032799.
O(d) for d decimal digits per test, with exponents bounded by d so each ipow is O(exponent) at worst; d is small for 32-bit ints.

🔄 Input / output examples

Swap inputNumber in Example 1 or widen the loop in Example 2.

nDisarium?Sum check
89Yes8¹ + 9² = 89
135Yes1¹ + 3² + 5³ = 135
10No1¹ + 0² = 1
7Yessingle digit

Edge cases and pitfalls

The reference intro does not spell out that positions are counted from the left; mixing that convention breaks the test.

Zero

countDigits(0)

Returning 0 skips the sum loop and can falsely report Disarium unless you special-case.

pow

pow(8, 1)

Might yield 7.9999999… style noise when cast back to int in pathological builds; integers are safer.

Overflow

Large n

Powers such as 99 still fit in 32-bit signed int, but wider digit counts need long long for the sum.

Armstrong

Different rule

An Armstrong number uses the same exponent (digit count) on every digit; do not merge the definitions in interview answers.

⏱️ Time and space complexity

OperationTimeExtra space
isDisarium(n)O(d · d) naively with ipow per digit of length dO(1)
Range [1, N]O(N · d2) worst caseO(1)

With cached powers per digit position, the per-n work drops to O(d).

Summary

  • Rule: left-to-right position p (from 1) raises the p-th digit to p.
  • Code: peel LSD while a position counter runs down from k.
  • Watch-outs: 0 digit count, pow typing, Armstrong confusion.
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