top of page
Writer's pictureSatyam Gupta

Disarium Number in JAVA

Disarium Number

A Disarium Number is a number if the sum of its digits powered with their respective position is equal with the number itself.

Example :  Input : 135

Process : 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135

Output : It is a Disarium Number


 

Algorithm and Program Below :-

Algorithm :-

  • Input a Number

  • Store it in another variable

  • Reverse the number.

  • Again Reverse the reversed number and store it in a new variable with sum of digits powered with their respective position

  • Compare the original number with this new number

  • If both are Equal than the number is Disarium Number

  • Else it is not a Disarium Number


 

Program :-


import java.io.*; import java.util.*; class Disarium {     public static void main(String args[])     {         Scanner in = new Scanner(System.in);         int n,p,i=0,rev,res,s=0,sum=0;         System.out.println("Enter Number");         n= in.nextInt(); // Entering Number         p=n; // Storing entered number in new variable         while(p>0)         {             rev= p%10;             s=s*10+rev; // reversing original number             p=p/10;         }         while(s>0)         {             res= s%10;             i++; // i variable used for power             sum=sum+(int)Math.pow(res,i); // storing sum of digits powered with their respective position             s=s/10;         }         if(sum==n) // comparing original no. with new number         {             System.out.println("Disarium Number : "+n);         }         else         {             System.out.println("Not a Disarium Number : "+n);         }     } // end of main } // end of class


 

Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.


All the Best :)

Keep Learning :)

82 views0 comments

Recent Posts

See All

Komentarze


Post: Blog2_Post
bottom of page