• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » Convert Decimal to Binary

Convert Decimal to Binary

Last Updated on: August 13, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Different Ways Of Decimal to Binary Converter in Java
  • Decimal to Binary How To Conversion Using toBinaryString() Method
  • Decimal to Binary How To Conversion Using Custom Method
  • Decimal to Binary How To Conversion Using Stack

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

We can convert decimal numbers to binary numbers by using the inverse technique that 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

We’re going to take a similar approach as discussed above to convert our Decimal Numbers to Binary Numbers using the Java program. We’ll be dividing the decimal number by 2 consecutively till we get 1, and storing the remainder in a variable with multiply of 10 at each step. Then we’ll print out the variable that stored the remainders – which is 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 how to convert the decimal number to binary numbers in various possible ways. If you are facing any difficulties in the program you can ask us in the comment section and we will happy to help you.

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers