Write a Program in Java to input a number and check whether it is a Disarium Number or not.
Answers
Answered by
2
import java.util.Scanner;
public class DisariumNumber {
static boolean isDisarium(int number) {
int sum = 0;
for (int i = number, pos = (number + "").length( ); i != 0;
sum += Math.pow(i % 10, pos--), i /= 10);
return sum == number;
}
public static void main(String[ ] args) {
System.out.print("Enter a number - ");
int number = new Scanner(System.in).nextInt( );
System.out.println((isDisarium(number) ? "A " : "Not a ") + "Disarium Number");
}
}
Similar questions