Java Program to Check Disarium Number

Program to Check Disarium Number

In this article we are going to understand what Disarium number is and how we can check whether a number is Disarium or not in Java with examples.

Disarium numbers are numbers where the digits raised to the power of their position order(from left to right) add upto the number itself.

Example:

135: (1)1+(3)2 +(5)3 =1+9+125= 135 is Disarium Number
121: (1)1 +(2)2 +(1)3 =1+2+1 = 4 is not Disarium Number

Where, (n)p represents power 'p' of number 'n‘.

In the above examples the numbers 135 and 518 are Disarium numbers. 121 digits’ with powers add upto 4, hence it is not a Disarium number.

Let’s see different ways to check disarium number.

Approach :

  1. We ask the user to enter a number or declare a number, which we store in two integer variables.
  2. We then use a while loop to iterate through all the digits in the number.
  3. All the digits are raised to power of their positions and added together.
  4. If the resultant sum is equal to the number then it is a disarium number else not.

Method-1: By Using Static Value

import java.util.Scanner;
public class DisariumNumber{
    public static void main(String args[])
    {
        //A number declared
        int num = 135;

        int temp=num,dig=0,remainder,sum=0;
        // Loop to count the number of dig
        while(temp>0)
        {
            dig++;
            temp=temp/10;
        }
        //Reassigning values to temp
        temp=num;
        //Adds up the digits with power
        while (temp > 0)
        {
            remainder = temp % 10;
            sum = sum + (int)Math.pow(remainder, dig);
            temp = temp / 10;
            dig--;
        }        

        if(sum==num)
        {
            System.out.println(num+" is a Disarium number");
        }
        else
        {
            System.out.println(num+" is not a Disarium number");
        }
    }
}
Output:

135 is a Disarium number

Method-2: By User Input Value

import java.util.Scanner;
public class DisariumNumber{
    public static void main(String args[])
    {
        //Taking the number as input from the user using scanner class
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter a number : ");
        int num = scan.nextInt();

        int temp=num,dig=0,remainder,sum=0;
        // Loop to count the number of dig
        while(temp>0)
        {
            dig++;
            temp=temp/10;
        }
        //Reassigning values to temp
        temp=num;
        //Adds up the digits with power
        while (temp > 0)
        {
            remainder = temp % 10;
            sum = sum + (int)Math.pow(remainder, dig);
            temp = temp / 10;
            dig--;
        }        

        if(sum==num)
        {
            System.out.println(num+" is a Disarium number");
        }
        else
        {
            System.out.println(num+" is not a Disarium number");
        }
    }
}
Output:

Case-1

Enter a number : 135
135 is a Disarium number

Case-2

Enter a number : 56
56 is a Disarium number

Leave a Comment