java program for disarium number
Java program for disarium number
In this article, we are going to check whether the given number is Disarium or not.
Introduction to Disarium number
When a number is equal to the sum of it’s of its digit raised to the power of with respect to its position is called Disarium number. Examples of Disarium numbers are 89, 135, 518, etc.
For example, 135 is a Disarium number as follows
In the following program, we have taken a number (135). First of all, we need to calculate the number of digits in the entered number. Then from number 135, 5 is taken as a reminder and raised its power to count (that is 3) in product = (int) Math.pow(reminder, count); and this product is added to sum and the next number will be done as the previous till the number become zero. After the then, the resultant number is checked with the original number. If it is equal to the original number then that number is a Disarium number.
import java.util.Scanner;
public class DisariumNumber
{
public static void main(String args[])
{
int num, product, sum, temp, count, reminder;
sum = count = 0;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a number to check Disarium Number:”);
num = sc.nextInt();
temp = num;
while(temp!=0)
{
temp /= 10;
count++;
}
temp = num;
while(temp != 0)
{
reminder = temp % 10;
product = (int) Math.pow(reminder ,count);
sum = sum + product;
temp /= 10;
count — ;
}
if(sum == num)
{
System.out.println(“Disarium Number”);
}
else
{
System.out.println(“Not a Disarium Number”);
}
}
}
Output:
Enter a number to check Disarium Number:
135
Disarium Number
Enter a number to check Disarium Number:
145
Not a Disarium Number
Disarium number with a method in java
In the following program, we have created two methods one is a constructor for initialization of instant variable and the other is disariumNumber() is for calculation of sum. And the sum is returned to main() method where it checked with the entered number. If the entered number is equal to the return number then this is called the Disarium number.
import java.util.Scanner;
public class DisariumNumberWithMethod
private int num; // for original number
private int product; // for taking power
private int sum; // for summation
private int temp; // for temporary number
private int count; // for count of total digits
private int reminder;// for taking reminder
// constructor for initialization
public DisariumNumberWithMethod()
// disariumNumber(int num) started
int disariumNumber(int num)
product = (int) Math.pow(reminder ,count);
public static void main(String args[])
DisariumNumberWithMethod ob = new DisariumNumberWithMethod();
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a number to check Disarium Number:”);
sumret = ob.disariumNumber(num);
System.out.println(“Disarium Number”);
System.out.println(“Not a Disarium Number”);
} // end of main() method
Originally published at https://javaknowhow.blogspot.com.