Disarium number program in java

A Disarium number is a number defined by the following process:

Sum of its digits powered with their respective position is equal to the original number. Some other DISARIUM are 89, 135,175, 518 etc

Input   : n = 135Output  : Yes 1^1 + 3^2 + 5^3 = 135Therefore, 135 is a Disarium number
import java.io.*;
class DisariumNumber
    {
    public static void main(String[] args)throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter a number : ");
            int n = Integer.parseInt(br.readLine());
            int copy = n, rem = 0, sum = 0;
            String s = Integer.toString(n); //converting the number into a String
            int len = s.length(); //finding the length of the number 
             
            while(copy>0)
            {
                rem = copy % 10; //extracting the last digit
                sum = sum + (int)Math.pow(rem,len);
                len--;
                copy = copy / 10;
            }
              if(sum == n)
                System.out.println(n+" is a Disarium Number.");
            else
                System.out.println(n+" is not a Disarium Number.");
        }
    }

Output:

Enter a number : 135
135 is a Disarium Number.
Enter a number : 121
121 is not a Disarium Number.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.