Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday, 21 September 2017

How to Convert Decimal to Binary in Java

Decimal to Binary Conversion in Java

Java Decimal to Binary Conversion

Here we will learn, How to convert decimal to binary in java with easy examples. Java decimal to binary conversion is the most important core java interview question.

There are many number conversion interview questions which is asked in java interviews like java program to convert decimal to hexadecimal, java program to convert decimal to octal, java program to convert binary to decimal, etc. But here we learn only decimal to binary conversion program in java. Let's start...

First, Let us understand what is decimal and binary number.

Decimal Numbers - Decimal number is the real number using the base 10. There are only 10 digits which represents numbers and starting from 0 to 9.

Binary Numbers - Binary number is number which contains only two digits 0 and 1 which know as bits.

Binary numbers in computer language i. e 0 and 1 represents true/false or on/off.

There are 3 ways, we can convert decimal to binary in java programs.

  • First is, Using toBinaryString() method of Integer class.
  • Second is, Using you own logic without any predefined method.
  • Third is, Using Stack. 

1) Decimal to Binary Example Using toBinaryString() method

class DecimalBinary
public static void main(String args[])
System.out.println("Binary representation of 1: ");
System.out.println("Binary representation of 6: ");
System.out.println("Binary representation of 12: ");
System.out.println("Binary representation of 45: ");

Output: Binary representation of 1:
             Binary representation of 6:
             Binary representation of 12:
             Binary representation of 45:

2) Decimal to Binary Example and count the number of 1s in binary numbers

In this program, we will take the input from user and convert it decimal to binary number and count 1s.

import java.util.Scanner;
class Counts
public static void main(String args[])
int n, a, count = 0;
String s = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
a = n%2;
if(a == 1)
s = s+" "+a;
n = n/2;
System.out.println("Binary number: "+s);
System.out.println("No. of 1s: "+count);

Output: Enter decimal number
             Binary number: 101101
             No of 1s: 4

3) Write a java program to convert decimal to binary without using predefined method

class Test
public void binaryConvert(int num)
int bin[] = new int[45];
int index = 0;
while(num > 0)
bin[index++] = num%2;
num = num/2;
for(int i = index-1; i >= 0; i--)
public static void main(String args[])
Test t = new Test();
System.out.println("Binary representation of 123: ");

Output: Binary representation of 123:

4) Another Example of java decimal to binary conversion

In this program, we create stack object and take input from the user for converting decimal to binary.

import java.util.*;
class Demo
public static void main(String args[])
Scanner sc = new Scanner(System.in);
//creating Stack object
Stack<Integer> stack = new Stack<Integer>();
System.out.println("Enter the decimal number: ");
int num = sc.nextInt();
while(num != 0)
int b = num % 2;
num /= 2;
System.out.print("\n Binary is: ");

Output: Enter the decimal number: 
             Binary is: 1000001

Read More:

How to Convert Binary to Decimal in Java.
How to convert Decimal to Hexadecimal in Java.
How to Convert Binary to Hexadecimal in Java.

Above all the examples of decimal to binary conversion programs in java are mostly asked in java interviews.



  1. Very useful java program. Thanks

  2. You have explained java programs very clearly.


Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts