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.

Java

Java Iterative Stmts

84 Likes

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

Answered By

30 Likes