Java Program to check for Disarium Number


Introduction


This program helps to check Disarium Number for a given number. In a simple word we can say “A number will be called Disarium if the sum of its digits powered with their respective position is equal with the number itself. “

A Disarium number is a number defined by the following process:
Sum of its digits powered with their respective position is equal to the original number.

Example:

135 is a Disarium number:
As 11+32+53 = 135

Other example will be 175, 89 & 518

 


Flowchart


Please find sample flowchart diagram for this program as given below –

 


Program (Code)


 

package com.kw.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This program helps to check Disarium Number for entered number. Example: 135
 * is a Disarium Number.
 *
 * @author dsahu1
 *
 */
public class KWDisariumNumberExample {

    public static void main(String[] args) throws IOException {

        // Instantiate BufferedReader object to get input from system
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // Enter Number
        System.out.print("Enter a number (n>0) : ");
        int num = Integer.parseInt(br.readLine());
        // Copy Original Number to temporary variable
        int temp = num;
        int d = 0;
        long sum = 0;
        // Convert Number to String to retrieve each digit
        String s = Integer.toString(num);
        // Retrieve the length of entered number
        int len = s.length();

        while (temp > 0) {
            // extracting the last digit
            d = temp % 10;
            // Calculate sum
            sum = sum + (int) Math.pow(d, len);
            len--;
            temp = temp / 10;
        }

        if (sum == num) {
            System.out
                    .println("***** " + num + " is a Disarium Number. ******");
        }

        else {
            System.out.println("***** " + num
                    + " is not a Disarium Number. ******");
        }

    }

}

 


Output


Scenario 1:

 

Enter a number (n>0) : 5345344
***** 5345344 is not a Disarium Number. ******

 

Scenario 2:

Enter a number (n>0) : 175
***** 175 is a Disarium Number. ******


Video Explanation


This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.

Please find video below –

 

One thought on “Java Program to check for Disarium Number”

Leave a Reply

Your email address will not be published. Required fields are marked *