Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Disarium Number

Updated 21 Oct 2026

A Disarium number is a number whose digits, each raised to the power of its position counted from the left, add up to the number itself. This program is written in the main method alone: it counts the digits, finds each power with a loop and adds them up.

Definition

A number $N$ with $k$ digits $d_1 d_2 \ldots d_k$ (from left to right) is a Disarium number if $d_1^1 + d_2^2 + \ldots + d_k^k = N$.

  • $135$: $1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135$. So 135 is a Disarium number.
  • $125$: $1^1 + 2^2 + 5^3 = 1 + 4 + 125 = 130$, which is not $125$. So 125 is not a Disarium number.

Algorithm

  1. Accept the number n using Scanner. If n <= 0, display an invalid input message and stop.
  2. Copy n to temp and count its digits in digits: while temp > 0, add 1 to digits and do temp = temp / 10.
  3. Copy n to temp again and set pos = digits and sum = 0. The digits are taken from the right, so the last digit has the position equal to digits.
  4. While temp > 0:
    • extract d = temp % 10;
    • find p, the value of d raised to the power pos, by multiplying p by d exactly pos times with a for loop;
    • add p to sum, decrease pos by 1 and do temp = temp / 10.
  5. If sum == n, display that n is a Disarium number. Otherwise display that it is not.

Program

Everything is written inside main: the digit count, the power found with an inner for loop, and the check of the sum.

import java.util.Scanner;
public class Disarium
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);        // reads input from the keyboard
        System.out.print("Enter a number: ");
        int n = sc.nextInt();                       // the number to be checked
        if (n <= 0)
        {
            System.out.println("Invalid input. Enter a positive number.");
        }
        else
        {
            int temp = n, digits = 0;               // digits counts the digits of n
            while (temp > 0)
            {
                digits++;
                temp = temp / 10;
            }
            temp = n;
            int pos = digits, sum = 0;              // pos: position of the digit, counted from the left
            while (temp > 0)
            {
                int d = temp % 10;                  // last digit
                int p = 1;                          // d raised to the power pos
                for (int i = 1; i <= pos; i++)
                {
                    p = p * d;
                }
                sum = sum + p;
                pos--;
                temp = temp / 10;
            }
            if (sum == n)
                System.out.println(n + " is a Disarium number.");
            else
                System.out.println(n + " is not a Disarium number.");
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the number typed by the user
nintThe number to be checked
tempintWorking copy of n whose last digit is removed in each pass (in the counting loop and in the main loop)
digitsintNumber of digits of n
posintPosition (counted from the left) of the digit being processed; starts at digits and decreases by 1
sumintRunning sum of each digit raised to the power of its position
dintThe last digit of temp in the current pass
pintThe digit d raised to the power pos, built by repeated multiplication
iintLoop counter of the for loop that finds the power

Sample input and output

Sample 1

Enter a number: 135
135 is a Disarium number.

Sample 2

Enter a number: 125
125 is not a Disarium number.

Sample 3

Enter a number: -4
Invalid input. Enter a positive number.

One value per line

Dry run

Take n = 135. The first loop counts the digits, so digits = 3. Then the main loop runs with temp = 135, pos = 3 and sum = 0.

Passtemp (start)dposp (d to the power pos)sumtemp (end)
11355312512513
2133291341
311111350

The loop stops as temp is 0. sum (135) equals n (135), so the program prints that 135 is a Disarium number.

Variations

  1. Disarium numbers in a range "Accept two numbers m and n and display all the Disarium numbers between m and n."
  2. Using recursive technique "Write a Disarium number program in which the power is found using a recursive method."
  3. Disarium numbers up to 1000 "Display all the Disarium numbers from 1 to 1000."