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
- Accept the number
nusingScanner. Ifn <= 0, display an invalid input message and stop. - Copy
ntotempand count its digits indigits: whiletemp > 0, add 1 todigitsand dotemp = temp / 10. - Copy
ntotempagain and setpos = digitsandsum = 0. The digits are taken from the right, so the last digit has the position equal todigits. - While
temp > 0:- extract
d = temp % 10; - find
p, the value ofdraised to the powerpos, by multiplyingpbydexactlypostimes with aforloop; - add
ptosum, decreaseposby 1 and dotemp = temp / 10.
- extract
- If
sum == n, display thatnis 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
n | int | The number to be checked |
temp | int | Working copy of n whose last digit is removed in each pass (in the counting loop and in the main loop) |
digits | int | Number of digits of n |
pos | int | Position (counted from the left) of the digit being processed; starts at digits and decreases by 1 |
sum | int | Running sum of each digit raised to the power of its position |
d | int | The last digit of temp in the current pass |
p | int | The digit d raised to the power pos, built by repeated multiplication |
i | int | Loop 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.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.
| Pass | temp (start) | d | pos | p (d to the power pos) | sum | temp (end) |
|---|---|---|---|---|---|---|
| 1 | 135 | 5 | 3 | 125 | 125 | 13 |
| 2 | 13 | 3 | 2 | 9 | 134 | 1 |
| 3 | 1 | 1 | 1 | 1 | 135 | 0 |
The loop stops as temp is 0. sum (135) equals n (135), so the program prints that 135 is a Disarium number.
Variations
- Disarium numbers in a range "Accept two numbers m and n and display all the Disarium numbers between m and n."
- Using recursive technique "Write a Disarium number program in which the power is found using a recursive method."
- Disarium numbers up to 1000 "Display all the Disarium numbers from 1 to 1000."