Computer Science, asked by Ritidubey, 1 year ago

write a program in java to check weather a number is disarium or not??​

Answers

Answered by srijansarkar123
0

Answer:

175 is a disarium number

Explanation:

public class DisariumNumber  

{  

   //calculateLength() will count the digits present in a number  

   public static int calculateLength(int n){  

       int length = 0;  

       while(n != 0){  

           length = length + 1;  

           n = n/10;  

       }  

       return length;  

   }  

     

   public static void main(String[] args) {  

       int num = 175, sum = 0, rem = 0, n;  

       int len = calculateLength(num);  

         

       //Makes a copy of the original number num  

       n = num;  

         

       //Calculates the sum of digits powered with their respective position  

       while(num > 0){  

           rem = num%10;  

           sum = sum + (int)Math.pow(rem,len);  

           num = num/10;  

           len--;  

       }  

         

       //Checks whether the sum is equal to the number itself  

       if(sum == n)  

           System.out.println(n + " is a disarium number");  

       else  

           System.out.println(n + " is not a disarium number");  

   }  

}  

Answered by samarthkrv
0

Answer:

public class DisariumNumber {

static int len(int n){

 int temp = n,len = 0;

  while(temp!=0){

   len++;

   temp/=10;

  }

return len;

}

static int[] toArray(int n){

 int temp = n , rev = 0 , index = 0;

 int[] arr = new int[len(n)];

 while(temp!=0){

  int last = temp%10;

  rev = rev*10 + last;

  temp /= 10;

 }

 while(rev!=0){

  int r = rev%10;

  arr[index] = r;

  index++;

  rev/=10;

 }

 return arr;

}

public static boolean isDisarium(int n) {

  int[] arr = toArray(n);

  int sum = 0;

   for(int i = 0; i < arr.length; i++){

     sum += Math.pow(arr[i] , i + 1);

   }

 return (sum == n);

}

}

Explanation:

Similar questions