Computer Applications
Write a program to input a number and find whether the number is a Disarium number or not. A number is said to be Disarium if sum of its digits powered with their respective positions is equal to the number itself.
Sample Input: 135
Sample Output: 135 ⇒ 11 + 32 + 53 = 135
So, 135 is a Disarium number.
Answer
import java.util.Scanner;
public class KboatDisariumNumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = in.nextInt();
int orgNum = num;
int digitCount = 0;
while (num != 0) {
num /= 10;
digitCount++;
}
num = orgNum;
int sum = 0;
while (num != 0) {
int d = num % 10;
sum += Math.pow(d, digitCount);
digitCount--;
num /= 10;
}
if (sum == orgNum)
System.out.println("Disarium Number");
else
System.out.println("Not a Disarium Number");
}
}Output
Related Questions
To execute a loop 10 times, which of the following is correct?
- for (int i=11;i<=30;i+=2)
- for (int i=11;i<=30;i+=3)
- for (int i=11;i<20;i++)
- for (int i=11;i<=21;i++)
Which of the following are entry controlled loops?
(a) for
(b) while
(c) do..while
(d) switch
- only a
- a and b
- a and c
- c and d
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);