Disarium Number in Java

In this post we will develop a Java program to check whether the given number is a Disarium number or not? We will also develop a Java program to find all the Disarium numbers in the given range.

A number whose sum of its digits powered with their respective position is equal to the original number is called disarium number. Examples of disarium numbers are- 135, 175, 518 and e.tc.

Number = 89 => 81 + 92 = 8 + 81 = 89 So, 89 is a disarium number. Number = 135 => 11 + 32 + 53 = 1 + 9 + 125 = 135 Hence 135 is a disarium number. Number = 518 => 51 + 12 + 83 = 5 + 1+ 512 = 518 So, 518 is a disarium number.

Procedure to develop a method to check the given number is disarium number or not,

1) Take a number
2) Store a copy of the number in a temporary variable
3) Declare a sum variable and initialize it with 0
4) Declare a variable digits to store the number of digits in the given number
5) Find the number of digits in the given number, and store results in the digits variable
6) Find the last digit of the number
7) Calculate lastDigit^digits and add it to the sum variable
8) Remove the last digit from the number
9) Decrease digits variable by 1
10) Repeat 6 to 9 steps until the number becomes 0.
11) Compare sum and the original number. if both are the same then the given number is disarium number else the given number is not a disarium number.

Program

import java.util.Scanner;

public class DisariumNumber {

   // method to check the Disarium number
   public static boolean isDisarium(int number){

      // declare variables
      int n = number; // temp variable
      int sum = 0;
      int digits = 0; // number of digits
      int lastDigit = 0;

      // calculate number of digits
      digits = countDigits(number);

      // iterate all digits of number
      while(n!=0) {

         // find last digit
         lastDigit = n % 10;

         // add result into sum
         sum += (int)Math.pow(lastDigit, digits);

         // remove last digit
         n /= 10;

         // decrease digits variable by 1
         digits--;
      }

      if(sum == number)
         return true;

      return false;
   }

   // method to count number of digits
   public static int countDigits(int number) {

      int number_of_digits = 0;
      while(number != 0) {

         number_of_digits++;
         // remove last digit
         number /= 10; 
      }

      return number_of_digits;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Disarium number or not
      result = isDisarium(number);

      // display result
      if(result)
         System.out.println(number +
                  " is a Disarium number.");
      else
         System.out.println(number +
                 " is not a Disarium number.");

      // close Scanner class object
      scan.close();
   }
}

The output for different test-cases are:-

Enter an integer number::135
135 is a Disarium number.

Enter an integer number::140
140 is not a Disarium number.

Enter an integer number::175
175 is a Disarium number.

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

The isDisarium(-) method also can be developed by using for loop. The below method is developed by using for loop.

public static boolean isDisarium(int number) {

   // declare variables
   int n = number; // temp variable
   int sum = 0;

   // count number of digits
   int digits = countDigits(number);

   // iterate through all digits of number
   for(int i=digits; n!=0; n/=10, i--) {

      // find last digit and
      // add into the sum variable
      sum += (int)Math.pow((n%10),i);
   }

   // compare sum and product
   if(sum == number)
      return true;

   return false;
}

Java program to find all Disarium number in the given range

import java.util.Scanner;

public class DisariumNumberInRange {

   // method to check the Disarium number
   public static boolean isDisarium(int number) {

      // declare variables
      int n = number; // temp variable
      int sum = 0;

      // count number of digits
      int digits = countDigits(number);

      // iterate through all digits of number
      for(int i=digits; n!=0; n/=10, i--) {

         // find last digit and
         // add into the sum variable
         sum += (int)Math.pow((n%10),i);
      }

      // compare sum and product
      if(sum == number)
         return true;

      return false;
   }

   public static int countDigits(int number) {
      int number_of_digits = 0;
      while(number != 0) {
         number_of_digits++;
         // remove last digit
         number /= 10; 
      }
      return number_of_digits;
   }

   // main method
   public static void main(String[] args) {

       // declare variables
       int minRange = 0, maxRange = 0;

       //create Scanner class object to take input
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter minimum value of range:");
       minRange = scan.nextInt();
       System.out.print("Enter maximum value of range:");
       maxRange = scan.nextInt();

       // loop
       System.out.println("The Disarium number from "+ 
              minRange + " to "+ maxRange+" are: ");

       for(int i=minRange; i<=maxRange; i++) {
           // check number
           if(isDisarium(i))
               System.out.print(i +" ");
       }

       // close Scanner class object
       scan.close();
    }
}

The output for the different test-cases are:-

Enter minimum value of range:1
Enter maximum value of range:1000
The Disarium number from 1 to 1000 are:
1 2 3 4 5 6 7 8 9 89 135 175 518 598

Enter minimum value of range:1000
Enter maximum value of range:10000
The Disarium number from 1000 to 10000 are:
1306 1676 2427

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *