Disarium Number
Disarium Number in JAVA
A Disarium Number is a number if the sum of its digits powered with their respective position is equal with the number itself.
Example : 11+32+53 = 135
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 then 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
- 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 then the number is Disarium Number
- Else it is not a Disarium Number
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
Comments
Post a Comment