Examples
- Java Program to Count Number of Digits in Number (Integer Length)
- Java Program to Reverse a String (4 Methods)
- How to Reverse an Array in Java? Array Reverse Program
- How to Find Power of a Number in Java? 4 Programs
- How to Find Factors of a Number in Java? Factors Program
- Check Disarium Number in Java (3 Easy Programs)
- Find Keith Number in Java (Easy Programs)
- Happy Number in Java (Easy Programs With Logic)
- Find Sunny Number in Java (Easy Programs)
Check Disarium Number in Java (3 Easy Programs)
In number theory, there exists a fascinating class of numbers known as "Disarium numbers." These numbers, though not as famous as their cousins like prime numbers or Fibonacci numbers, possess a unique and interesting property that makes them worth exploring.
What is Disarium Number?
A disarium number is a number that is equal to the sum of its digits raised to the power of their respective positions in the number.
In other words, if a number is written as abcd..., where a, b, c, d, etc., are its digits, then it is considered a disarium number if it satisfies the following equation:
a^1 + b^2 + c^3 + d^4 + ... = abcd...
Here are a few examples of disarium numbers:
-
89 is a disarium number because:
8^1 + 9^2 = 8 + 81 = 89
-
135 is a disarium number because:
1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135
-
518 is a disarium number because:
5^1 + 1^2 + 8^3 = 5 + 1 + 512 = 518
Not all numbers are disarium numbers, and they are relatively rare.
In this tutorial, we will learn about the Disarium number in Java, and how to identify them with a Java program.
Concepts to Learn:
Check Disarium Number in Java Using while loop
Here's how you can check for Disarium numbers in Java using a while loop:
Code
public class DisariumNumberCheck {
public static void main(String[] args) {
int number = 89; // Example number to check
int originalNumber = number; // Store the original number
int sum = 0;
int numberOfDigits = (int) Math.log10(number) + 1; // Calculate the number of digits
while (number > 0) {
int digit = number % 10; // Extract the last digit
sum += Math.pow(digit, numberOfDigits); // Add the digit raised to the power of its position
number /= 10; // Move to the next digit
numberOfDigits--; // Decrement the count of digits
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is a Disarium number.");
} else {
System.out.println(originalNumber + " is not a Disarium number.");
}
}
}
Output
89 is a Disarium number.
Explanation
We start with a number to check (in this example, 89) and store its original value.
We calculate the number of digits in the number using Math.log10(number) + 1. This gives us the count of digits, which is essential for the Disarium number check.
Using a while loop, we extract each digit from the number from right to left, calculate the digit raised to the power of its position (1-based), and accumulate these values in the sum variable.
After processing all the digits, we check if the sum is equal to the original number. If they are equal, we conclude that the original number is a Disarium number; otherwise, it is not.
Disarium Number Program in Java Using Recursion
Here's how to check for Disarium numbers in Java using recursion:
Code
public class DisariumNumberCheck {
public static void main(String[] args) {
int number = 598; // Example number to check
if (isDisarium(number, number, 0)) {
System.out.println(number + " is a Disarium number.");
} else {
System.out.println(number + " is not a Disarium number.");
}
}
static boolean isDisarium(int number, int originalNumber, int sum) {
if (number == 0) {
return sum == originalNumber;
}
int digit = number % 10;
return isDisarium(number / 10, originalNumber, sum + (int) Math.pow(digit, (int) Math.log10(originalNumber) + 1));
}
}
Output
598 is a Disarium number.
Explanation
We start with a number to check (in this example, 598).
We call the isDisarium method with three parameters: number (the current number being processed), originalNumber (the original number we want to check), and sum (the running total of the digits raised to the power of their position).
In the isDisarium method, we check if number has reached 0. If it has, we compare the sum with the originalNumber to determine if it's a Disarium number. If they are equal, we return true; otherwise, we return false.
If number is not 0, we extract the last digit using number % 10, calculate the digit raised to the power of its position, and add it to the sum. We then make a recursive call with the remaining digits.
The recursion continues until all digits have been processed, and the final result indicates whether the original number is a Disarium number.
Check Disarium Numbers in a Given Range in Java
Here's a Java program to check disarium numbers in a given range and prints the results:
Code
public class DisariumNumbers {
public static void main(String[] args) {
int startRange = 1; // Define the start of the range
int endRange = 1000; // Define the end of the range
System.out.println("Disarium numbers from " + startRange + " to " + endRange + ":");
for (int i = startRange; i <= endRange; i++) {
if (isDisarium(i)) {
System.out.println(i);
}
}
}
// Function to check if a number is a disarium number
public static boolean isDisarium(int num) {
String numStr = String.valueOf(num);
int sum = 0;
for (int i = 0; i < numStr.length(); i++) {
int digit = Integer.parseInt(String.valueOf(numStr.charAt(i)));
sum += Math.pow(digit, i + 1);
}
return sum == num;
}
}
Output
Disarium numbers from 1 to 1000:
1
2
3
4
5
6
7
8
9
89
135
175
518
598
Explanation
We define the startRange and endRange to specify the range of numbers within which we want to find disarium numbers.
We use a for loop to iterate through the numbers in the specified range.
For each number in the range, we call the isDisarium function to check if it is a disarium number.
Inside the isDisarium function, we convert the number to a string to work with its digits. We calculate the sum of each digit raised to the power of its position using a loop.
If the sum is equal to the original number, we return true, indicating that it's a disarium number. Otherwise, we return false.
Learn Next: