Decimal to binary converter

Below is example java program decimal to binary converter which will convert any decimal number to its binary form and shows output on the console.

package com.javahonk.shiftoperator;

public class DecimalToBinaryConvert {

	public static void main(String[] args) {
		System.out.println(decimalToBinary(46));

	}

	private static Object decimalToBinary(int decimalNumber) {
		int remainderNumber;

		if (decimalNumber <= 1) {
			System.out.print(decimalNumber);
			return "";
		}

		remainderNumber = decimalNumber % 2;
		decimalToBinary(decimalNumber >> 1);
		System.out.print(remainderNumber);
		{
			return "";
		}
	}
}

 

Output:

Decimal to binary converter

 

That’s it for Decimal to binary converter in java.

Leave a Reply

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