Tuesday, February 7, 2017

Write a Java Program which takes an input and check whether it is a Disarium Number or not.

Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal
to the original number.
For example 135 is a DISARIUM
(Workings 1*1+3*2+5*3 = 135, some other DISARIUM are 89, 175, 518 etc)  */

import java.io.Console;
import static java.lang.System.out;
class Disarium
{
static public void main(String...alt)
{
Console c=System.console();
String s=c.readLine("Enter a Number : ");
int n=Integer.parseInt(s),temp=n,sum=0,rem=0;  // Comma(,) has operator left to right Associativity
int l=s.length();
while(n!=0)
{
rem=n%10;
sum+=Math.pow(rem,l);
n/=10;
l--;
}
if(sum==temp)
out.println(s+" is Disarium No");
else
out.println(s+" is not Disarium no");
}
}

Share this

0 Comment to "Write a Java Program which takes an input and check whether it is a Disarium Number or not."

Post a Comment