In this tutorial you will learn about the Program to check Disarium Number In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Disarium Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
- Inbuilt functions.
What is In Disarium Numbers?
A number is called Disarium number if sum of its power at the positions from left to right is equal to the number.
Example:
11 + 32 + 53 = 1 + 9 + 125 = 135
Program to check Disarium Number In Java
In this program we will find given number is a Disurium number or not in using java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the a number.Later we find number is Sisarium number or not let’s have a look at the 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 |
//Java program to find Disarium Number.. import java.util.Scanner; public class Disarium{ public static void main(String args[]) { //Declaring variable and taking value from user... Scanner sc = new Scanner(System.in); System.out.print("Please enter a number : "); int number = sc.nextInt(); int temp = number, a = 0, sum = 0; String s = Integer.toString(number); int l = s.length(); // checking condition for Disauram number while(temp>0) { a = temp % 10; sum = sum + (int)Math.pow(a,l); l--; temp = temp / 10; } // if they are equal number Disauram number.. if(sum == number) System.out.println("Disarium Number."); else System.out.println("Not a Disarium Number."); } } |
Output
Disarium Number
Not a Disarium Number
In the above program, we have first declared and initialized a set variables required in the program.
- number= it will hold entered number
- temp= it will hold temporary origianal number
- sum= it will hold sum of values
- l= it will hold length of a number
After declaring variables in the next statement user will be prompted to enter a value and which will be assigned to variable ‘number’.
And after that we calculate the value Sum of its digits powered with their respective position is equal to the original number or not using Math.pow(a,l). inbuilt function
So the given number called Disarium if “sum of its digits powered with their respective position are equal” with the number.
Number is Disarium if condition True.
If not it is not a Disarium Number.