SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Disarium Number Program in Java and Python

28 August 2014

Disarium Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Disarium Number or not.

Disarium Number: A number is called a Disarium Number if the sum of its digits raised to the power of their respective positions is equal to the original number.

Example: 135 is a Disarium Number because: 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Store a copy of the number in another variable.

Step 4: Find the number of digits and store it as the current position.

Step 5: Initialize sum to 0.

Step 6: Extract the last digit of the copy.

Step 7: Add the digit raised to the current position to sum.

Step 8: Decrease the position by 1 and remove the last digit from the copy.

Step 9: Repeat Steps 6 to 8 until the copy becomes 0.

Step 10: If sum is equal to the original number, display that it is a Disarium Number; otherwise, display that it is not a Disarium Number.

Step 11: Stop.

Explanation:

The program checks whether a number is a Disarium number by using digit positions. In a Disarium number, each digit is raised to the power of its position from left to right, and the sum of these powers must be equal to the original number. For example, in a three-digit number, the hundreds digit has position 1, the tens digit has position 2, and the units digit has position 3.

The Java code extracts digits from right to left because % 10 gives the last digit first. To still use the correct position, the program first finds the total number of digits by converting the number to a string and taking its length. This length is stored in position, which is the power for the last digit. After each digit is processed, position is decreased by 1.

The variable copy is used for extraction so that the original number n remains available for the final comparison. In each loop pass, digit = copy % 10 extracts the current digit, Math.pow(digit, position) calculates its positional power, and this value is added to sum. Then copy / 10 removes the digit. When all digits are processed, sum is compared with n. Equality means the number satisfies the Disarium condition.

Java Program:

Java
/**
* The class Disarium accepts a number and checks whether it is
* a Disarium Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class Disarium
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        int copy = n;
        int sum = 0;

        /*
        * The length gives the position of the last digit because
        * digits are extracted from right to left.
        */
        String s = Integer.toString(n);
        int position = s.length();

        while(copy > 0)
        {
            int digit = copy % 10;
            sum = sum + (int)Math.pow(digit, position);
            position--;
            copy = copy / 10;
        }

        if(sum == n)
        System.out.println(n + " is a Disarium Number");
        else
        System.out.println(n + " is not a Disarium Number");
    }
}

Equivalent Python Program:

Python
# Read the required input values for the program.
# Process the data using loops, conditions and helper logic.
# Display the final result in the required format.

n = int(input("Enter a number: "))

copy = n
total = 0

# The length gives the position of the last digit.
position = len(str(n))

# Extract digits from right to left.
while copy > 0:
    digit = copy % 10
    total = total + (digit ** position)
    position = position - 1
    copy = copy // 10

if total == n:
    print(n, "is a Disarium Number")
else:
    print(n, "is not a Disarium Number")

Output:

Enter a number: 135 135 is a Disarium Number Enter a number: 219 219 is not a Disarium Number Enter a number: 89 89 is a Disarium Number

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →