Input a number from keyboard and check whether it is Disarium or not.
What is Disarium Number
A number will be called Disarium if sum of its digits
powered with their respective position is equal with the number itself.
Sample
Input: 135
Output : It is a Disarium
(1^1+3^2+5^3 = 135, some other Disarium are 89, 175, 518
etc)
class Disarium
{
public void show(int n)
{
int c=1,rev,sum,x;
x=n;
sum=0;
rev=0;
while (n>0)
{
rev=rev*10+n%10;
n=n/10;
}
while(rev>0)
{
sum=sum+(int)Math.pow(rev%10,c);
c++;
rev=rev/10;
}
if(sum==x)
System.out.println(x + " is DISARIUM");
else
System.out.println(x + " is not DISARIUM");
}
public static void main(String args[]) throws Exception
{
Disarium ob=new Disarium ();
ob.show(275);
}
}
No comments:
Post a Comment