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(Integer.toBinaryString(1));
System.out.println("Binary representation of 6: ");
System.out.println(Integer.toBinaryString(6));
System.out.println("Binary representation of 12: ");
System.out.println(Integer.toBinaryString(12));
System.out.println("Binary representation of 45: ");
System.out.println(Integer.toBinaryString(45));
}
}

Output: Binary representation of 1:
              1
             Binary representation of 6:
             110
             Binary representation of 12:
             1100
             Binary representation of 45:
             101101


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();
while(n>0)
{
a = n%2;
if(a == 1)
{
count++;
}
s = s+" "+a;
n = n/2;
}
System.out.println("Binary number: "+s);
System.out.println("No. of 1s: "+count);
}
}

Output: Enter decimal number
             45
             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--)
{
System.out.print(bin[i]);
}
}
public static void main(String args[])
{
Test t = new Test();
System.out.println("Binary representation of 123: ");
t.binaryConvert(123);
}
}

Output: Binary representation of 123:
             1111011


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;
stack.push(b);
num /= 2;
}
System.out.print("\n Binary is: ");
while(!(stack.isEmpty()))
{
System.out.print(stack.pop());
}
System.out.println();
}
}

Output: Enter the decimal number: 
             65
             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.

Share:

3 comments:

  1. Very useful java program. Thanks

    ReplyDelete
  2. You have explained java programs very clearly.
    Thanks,
    https://www.flowerbrackets.com/java-decimal-to-binary-using-tobinarystring-and-stack/

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate