Convert Decimal to Binary

Decimal to Binary How To Conversion: In our previous blog post on the Java Programs For Beginners series, we discussed and explained Binary To Decimal and How To do it. But in this post, we will do the reverse operation, which means converting Decimal to Binary.

We can convert decimal numbers to binary numbers using the inverse technique we used to convert binary to decimal. To do this, we take the decimal number and divide it by 2 repeatedly until we get 1. We also store the remainder in a variable multiplied by 10 at each step. Finally, you have to print the variable which stores remainders.

For a Better understanding of these programs, you must have some programming knowledge on Java’s few topics like:

  • Java Methods
  • Java Operators
  • Java while Loop
Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

Different Ways Of Decimal to Binary Converter in Java

We can convert a decimal number into a binary number by following any of the below methods:

  • Using toBinaryString() method
  • Using custom method
  • Using Stack

Decimal to Binary How To Conversion Using toBinaryString() Method

We’re using the toBinaryString() method from the Integer wrapper class. This handy little method takes an integer argument and returns a string representation of it as an unsigned integer in binary base2. So, if we pass it a decimal number, it’ll give us back its binary equivalent value as a String.

package com.softwaretestingo.basic;
import java.util.Scanner;
public class DecimaltoBinaryEx1 
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter a Decimal Number : ");

		// input decimal number
		int decimalNumber = sc.nextInt();

		// passing decimal number in Integer.toBinaryString() method
		// converting decimal to binary, return type String
		String binaryNumber = Integer.toBinaryString(decimalNumber);		

		// printing the values of decimal and binary equivalent
		System.out.println("Decimal Number : " + decimalNumber);
		System.out.println("Binary Equivalent : " + binaryNumber);
	}
}

Output:

Enter a Decimal Number : 15
Decimal Number : 15
Binary Equivalent : 1111

Decimal to Binary How To Conversion Using Custom Method

Using the Java program, we will take a similar approach as discussed above to convert our Decimal Numbers to Binary Numbers. We’ll divide the decimal number by 2 consecutively until we get 1, storing the remainder in a variable with a multiplier of 10 at each step. Then, we’ll print out the variable that stored the remainders—the binary equivalent of the decimal number.

package com.softwaretestingo.basic;
import java.util.Scanner;
public class DecimaltoBinaryEx2 
{
	public static void main(String[] args) 
	{
		Scanner scn = new Scanner(System.in);

		System.out.println("Enter a Decimal Number : ");

		// input a decimal number
		long decimalNumber = scn.nextLong();

		// calling the method DecimalToBinary and storing it in binaryNumber 
		long binaryNumber = DecimalToBinary(decimalNumber);

		// printing the values of decimal number and binary equivalent
		System.out.println("Decimal Number: " + decimalNumber);
		System.out.println("Binary Equivalent: " + binaryNumber);
	}
	public static long DecimalToBinary(long n) 
	{

		long binaryNumber = 0;
		long remainder;
		int i = 1, step = 1;

		while (n!=0) 
		{
			remainder = n%2;
			System.out.println("Step " + step++ + ": " + n + "/2");

			System.out.println("Quotient = " + n/2 + ", Remainder = " + remainder);
			n /= 2;

			binaryNumber += remainder * i;
			i *= 10;
		}

		return binaryNumber;
	}
}

Output:

Enter a Decimal Number :  15
Step 1: 15/2
Quotient = 7, Remainder = 1
Step 2: 7/2
Quotient = 3, Remainder = 1
Step 3: 3/2
Quotient = 1, Remainder = 1
Step 4: 1/2
Quotient = 0, Remainder = 1
Decimal Number: 15
Binary Equivalent: 1111

Decimal to Binary How To Conversion Using Stack

package com.softwaretestingo.basic;
import java.util.Scanner;
import java.util.Stack;
public class DecimaltoBinaryEx3 
{
	public static void main(String[] args) 
	{
		// Create Stack object
		Stack<Integer> stack = new Stack<Integer>();

		Scanner sc = new Scanner(System.in);
		// User input 
		System.out.println("Enter decimal number: ");
		int num = sc.nextInt();

		while (num != 0)
		{
			int d = num % 2;
			stack.push(d);
			num /= 2;
		} 
		System.out.print("\nBinary representation is:");
		while (!(stack.isEmpty() ))
		{
			System.out.print(stack.pop());
		}
		System.out.println();
	}
}

Output:

Enter decimal number: 15
Binary representation is:1111

Conclusion:

We have discussed in detail various ways to convert decimals to binary numbers. If you are facing any difficulties with the program, you can ask us in the comment section, and we will be happy to help you.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment