**What is Disarium Number?**

Consider a number such as 135. Its digits are 1, 3 and 5 with 1 at position 1, 3 at position 2 and 5 at position 3 in the number. Now, raise the digits to the power of their position and add the resultant values, which means 1^{1} + 3^{2} + 5^{3} = 1 + 9 + 125 = 135 which is the same as the number itself. Such numbers whose sum of digits raised to the power of their position is equal to the number are called **Disarium Numbers.**

*Note that the position is counted starting from 1 and from the left of the number.
*

If the sum of the values obtained by raising the digits of a number to the power of their position is equal to the number itself, then the number is called **Disarium Number**.

**Java Program**

Program to check for a Disarium number is given below

import java.util.Scanner; public class DisariumNumberChecker { public static void main(String[] args) { // initialize reader to read user input Scanner reader = new Scanner(System.in); System.out.println("Enter a number"); String numberStr = reader.nextLine(); // close reader reader.close(); // calculate length of entered number int length = numberStr.length(); // convert entered string to integer int number = Integer.parseInt(numberStr); // initialize variable to hold sum int sum = 0; // make a copy of number so that original number can be compared with // the sum at the end int copy = number; while (copy > 0) { // get the rightmost digit int digit = copy % 10; // raise the digit to its position. Position will be the length of // the number int digitRaisedToPower = (int) Math.pow(digit, length); // add it to sum sum += digitRaisedToPower; // remove rightmost digit copy = copy / 10; // decrease length by 1 length--; } // compare sum with number to check if (sum == number) { System.out.println("Entered number is a Disarium number"); } else { System.out.println("Entered number is not a Disarium number"); } } } |

**Output**

Enter a number

135

Entered number is a Disarium number

Enter a number

125

Entered number is not a Disarium number

**Logic**

Number is read from the user and its length is calculated. Initially, length will be the number of digits. A variable `sum`

is initialized to 0 to hold the sum of digits raised to the power of position. Note that a copy of original number is made and all operations are performed on the copy so that the actual number is still with us for comparison with the sum later.

**Now we have to get individual digits of the number so that power raised to their position can be calculated.**

For getting the digits, modulus operator(%) is used and number % 10 is calculated. *Number % 10 gives the rightmost digit of a number. *This digit is raised to the power of length since length of the number will be the position of rightmost digit. Power is calculated using `pow`

method of `java.lang.Math`

class and the result is added to the `sum`

variable.

Since the rightmost digit is now processed, it is removed using the division operator(/). *Number / 10 removes the rightmost digit and gives the remaining number*. **Example**, 135 /10 gives 13. Length is also decreased by 1 since now there is one digit less in the number. Again, the rightmost digit is determined and power of its position is calculated and added to the sum as above.

**Since this operation has to be repeated for all the digits of the number, it is enclosed in a loop**. The condition of the loop is `number > 0`

. Number will become 0 when there will be only 1 digit left in the number because then “number / 10” will return 0.

After the loop completes, the original number is compared with the resultant sum. If they are same, the number is Disarium.

**Let’s tweak in :**

- Modulus(%) operator gives the remainder of division. Thus 135 % 10 = 5.
- Division(/) operator gives the quotient of division. Thus 135 / 10 = 13.

Different explanation of disarium number from those available everywhere. Very nice.

Thanks Shweta…Keep visiting !!!