Disarium Number Program in Java with Examples

In this post I will be sharing what is Disarium number, examples of Disarium number , algorithm and program in java to check whether a given number is Disarium or not.

Read Also :  Happy Number Program in Java

What is Disarium Number

Disarium number can be defined as a number whose sum of digits, powered with their respective position, is equal to the original number.

Examples:

Below are examples of numbers which are Disarium

Number to check : 89 
81+92 = 8 + 81 = 89           // 89 is a Disarium Number
 
Number to check : 135 
11+32+53 = 1 + 9 + 125 = 135  //135 is a Disarium Number
 
Number to check : 175 
11+72+53 = 1 + 49 + 125 = 175 //175 is a Disarium Number
 
Number to check : 518 
51+12+83= 5 + 1 + 512 = 518   //518 is a Disarium Number

Below are examples of numbers which are NOT Disarium

Number to check : 19 
11+92 = 1 + 81 = 82           // 19 is not a Disarium Number
 
Number to check : 134 
11+32+43 = 1 + 9 + 64 = 74    //134 is not a Disarium Number
 
Number to check : 176 
11+72+63 = 1 + 49 + 216 = 266 //176 is not a Disarium Number
 
Number to check : 512  
51+12+23= 5 + 1 + 8 = 14      //512 is not a Disarium Number


Read Also :  Pronic Number Program in Java

Java Program for Disarium Number


import java.util.*;

public class JavaHungry {
public static void main(String args[]) {
        System.out.println("Input number : ");
Scanner scan = new Scanner(System.in);
int inputNumber = scan.nextInt();
Boolean result = checkDisarium(inputNumber);
if (result == true)
System.out.print(inputNumber + " is a Disarium number");
else
System.out.print(inputNumber + " is not a Disarium number");
}

public static boolean checkDisarium(int inputNumber) {
        //Calculate number of digits in the inputNumber
int numOfDigits = Integer.toString(inputNumber).length();

// Initialize sumOfDigits of inputNumber
int sumOfDigits = 0;

// Create a copy of the inputNumber
int num = inputNumber;

/* Calculate the sum of digits powered
with their respective position */
while (num != 0) {
// Get the rightmost digit
int currentDigit = num % 10;
sumOfDigits = (int) (sumOfDigits + Math.pow(currentDigit, numOfDigits));
numOfDigits--;
num = num / 10;
}
/* If sumOfDigits is same as inputNumber then
the number is Disarium, otherwise not */
return (sumOfDigits == inputNumber);
}
}

Output :
Input number : 135
135 is a Disarium number 

Algorithm for Disarium Number

1. Calculate number of digits present in the inputNumber(original number) by using length() method.

2. Initialize sumOfDigits variable to 0 as it will represent the sum of the digits powered with their respective position.

3. Create a copy of inputNumber (original number) by storing its value in variable num.

4. Using while loop

     a. Get the rightmost digit of variable num by using (num % 10). Store its value in variable    currentDigit.
     b. Use Math.pow() function and calculate the value of currentDigit raised to power its position.
     c. add the value of Math.pow() function to sumOfDigits variable. 

5. Check whether the variable sumOfDigits is equal to inputNumber (original number).

     a. If both are equal then the inputNumber is Disarium.
     b. Otherwise, the inputNumber is not a Disarium number.

That's all for the day , please mention in comments if you know any other way to implement Disarium number program in java.

0 Comments