Write a simple java program to verify if a given number is **disarium number** or not.

## 1. Disarium number

A number is called DISARIUM if sum of its digits, powered with their respective position, is equal to the original number.

For example, consider following numbers.

1

^{1}+ 3^{2}+ 5^{3}= 1 + 9 + 125 = 1358

^{1}+ 9^{2}= 8 + 81 = 891

^{1}+ 7^{2}+ 5^{3}= 1 + 49 + 125 = 1755

^{1}+ 1^{2}+ 8^{3}= 5 + 1 + 512 = 518

## 2. Java Program to find Disarium number

public class Main { public static void main(String[] args) { System.out.println("134 is disarium number " + isDisarium(134)); System.out.println("135 is disarium number " + isDisarium(135)); System.out.println("136 is disarium number " + isDisarium(136)); } static boolean isDisarium(int originalNumber) { //Total number of digits int numberOfDigits = Integer.toString(originalNumber).length(); int sumOfDigits = 0; // Initialize sum of terms int tempNum = originalNumber; while (tempNum!=0) { // Get the rightmost digit int currentDigit = tempNum % 10; // powering according to the positions and adding to sumOfDigits sumOfDigits = (int) (sumOfDigits + Math.pow(currentDigit, numberOfDigits--)); tempNum = tempNum/10; } // If sum is same as number, then number is return (sumOfDigits == originalNumber); } }

Program output.

134 is disarium number false 135 is disarium number true 136 is disarium number false

Happy Learning !!

## Leave a Reply