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 number are 89, 135,
518 etc.
For example, 135 is a
Disarium number as follows
11 + 32
+ 53 = 1 + 9 + 125 = 135
In the
following the 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
Programming with Java by
Understanding ISC Computer Science (Java with Bluej) by
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()
{
sum = 0;
count = 0;
}
// disariumNumber(int num) started
int disariumNumber(int num)
{
this.num = num;
temp = num;
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--;
}
return sum;
} // end of the method
public static void main(String args[])
{
int num, sumret;
DisariumNumberWithMethod ob = new DisariumNumberWithMethod();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number
to check Disarium Number:");
num = sc.nextInt();
// method call
sumret = ob.disariumNumber(num);
if(num == sumret)
{
System.out.println("Disarium
Number");
}
else
{
System.out.println("Not a
Disarium Number");
}
} //
end of main() method
}