Disarium Number Program in Java

In this tutorial we will learn what is disarium number in java and write disarium number program in java .

Disarium number

A number is called Disarium number if and only if the sum of its power of the positions from left to right is equal to the number.

Example of disarium number is 135 because

11 + 3 2 + 53 = 1 + 9 + 125 = 135

Java Program for disarium number

In this program we check given number is disarium number in java or not . There are steps as following

  1. Take input from user as num .
  2. Count the digits of num.
  3. Now we will assign num to temp and iterate it till temp > 0 .
  4. Now each iteration we calculate reminder rem of temp when divided by 10 and raised rem with power of digits and add to sum . Then divided temp with 10 and result assign to temp and decrement value of digits with 1.
  5. If sum is equal to num then given number is a disarium number .

 

import java.util.Scanner;

public class DisariumNumber {

	public static void main(String[] args) {
		int rem, num, temp, digits = 0, sum = 0;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter number :");
		num = sc.nextInt();
		temp = num;
		while (temp > 0) {
			digits++;
			temp = temp / 10;
		}
		temp = num;
		while (temp > 0) {
			rem = temp % 10;
			sum = sum + (int) Math.pow(rem, digits);
			temp = temp / 10;
			digits--;
		}

		if (num == sum) {
			System.out.println("Given nubmer is Disarium Number");
		} else {
			System.out.println("Given number is not Disarium Number");
		}
	}

}

Output :

Enter number :135
Given nubmer is Disarium Number

In this tutorial we have learned what is disarium number in java and write java program to check given number is disarium number or not . you can visit java coding program for interview preparation .

Leave a Reply

Your email address will not be published. Required fields are marked *

57 − 51 =