Java program to check the DISARIUM number | JAVA POINT

 

Write a Java program to check that a given number is  DISARIUM number or not.



DISARIUM number



DISARIUM number

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


PROGRAM :


import java.util.Scanner;

class disarium

  public static void main(String[] args)

  {

   Scanner sc=new Scanner(System.in);

   System.out.print("\nEnter the number to be checked : ");

   int n=sc.nextInt();


   int copy = n, d = 0, sum = 0;

   String s = Integer.toString(n); //converting the number into a String

   int len = s.length(); //finding the length of the number i.e. no.of digits

       

   while(copy>0)

   {

    d = copy % 10; //extracting the last digit

    sum = sum + (int)Math.pow(d,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 the number to be checked : 135

135 is a Disarium Number.

(Workings 1+3*3+5*5*5 = 1+9+125 = 135

other DISARIUM are 89, 175, 518 etc)

*/