Search Your Topic

config
Tuesday, 18 July 2017 06:30

Java Program to check for Disarium Number

Written by
Rate this item
(0 votes)

Question:

Write a Program in Java to input a number and check whether it is a Disarium Number or not.

Note: A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number.

For example 135 is a DISARIUM
(Workings 11+32+53 = 135, some other DISARIUM are 89, 175, 518 etc)

 

Programming Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* The class Disarium inputs a number and checks whether
* it is a Disarium number or not
* @author : www.anishsir.in
* @Program Type : BlueJ Program - Java
*/
 
import java.io.*;
class Disarium
    {
    public static void main(String[] args)throws IOException
        {
            BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Enter a number : ");
            int n = Integer.parseInt(br.readLine());
            int copy = n, d = 0, sum = 0;
            String s = Integer.toString(n); //converting the number into a String
            int len = s.length(); //finding the length of the number i.e. no.of digits
             
            while(copy>0)
            {
                d = copy % 10; //extracting the last digit
                sum = sum + (int)Math.pow(d,len);
                len--;
                copy = copy / 10;
            }
             
            if(sum == n)
                System.out.println(n+" is a Disarium Number.");
            else
                System.out.println(n+" is not a Disarium Number.");
        }
    }

 

Output:

Enter a Number : 135
135 is a Disarium Number.

Enter a Number : 219
219 is not a Disarium Number.

Enter a Number : 89
89 is a Disarium Number.

Read 677 times Last modified on Tuesday, 18 July 2017 06:39
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook