Decimal to binary conversion using Java
Decimal to Binary conversion using Java
In this article we will create program for decimal to binary conversion using java. For this purpose we need to ask a decimal number from user and convert that decimal number to its binary equivalent form and then print the converted number on to the screen.

Working
For conversion, we divide the decimal number by 2 and get the quotient
for the next iteration and continuously store the remainder value in an
array in reverse order till the decimal number is greater than 0. For better understanding with an example just have a look towards the diagram
shown at the right.
for the next iteration and continuously store the remainder value in an
array in reverse order till the decimal number is greater than 0. For better understanding with an example just have a look towards the diagram
shown at the right.
Example : Decimal number 5 = 101 in binary representation.

Algorithm
- Initialize a empty array name binary
- While decimal is greater then zero
- If decimal is even r equals to 0 and else set 1
- Append r to binary
- Set decimal as half of its value
Java Code
Run
//Java program to convert decimal number to binary number public class Main { public static void main(String args[]) { //Decimal Number int decimal = 12; //integer array for storing binary digits int binary[] = new int[20]; int i = 0; //writing logic for the conversion while(decimal > 0) { int r = decimal % 2; binary[i++] = r; decimal = decimal/2; } //printing result System.out.print("Binary number : "); for(int j = i-1 ; j >= 0 ; j--) System.out.print(binary[j]+""); } }
Output
Binary number : 1100
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
package pripInsta;
public class BinarytoDecimal {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str=”1110″;
System.out.println(“in Binary: “+str);
int n=Integer.parseInt(str, 4);
System.out.println(“in Decimal: “+n);
int x=22;
System.out.println(“Decimal “+x);
String s2=Integer.toBinaryString(x);
System.out.println(“to binary “+s2);
}
}
Hey there, Kindly join our Discord server for all the technical and subject related queries.
import java.text.NumberFormat.Style;
import java.util.*;
public class Perfect{
public static List decimaltobinary(int decimal){
List a = new ArrayList();
while(decimal >0){
int rem = decimal %2;
a.add(rem);
decimal /=2;
}
Collections.reverse(a);
return a;
}
public static void main(String[] args){
List hold;
hold =decimaltobinary(6);
System.out.println(hold);
}
}
// Without Using extra space
import java.util.Scanner;
public class a_31_Decimal_to_Binary {
public static int binary(int dec) {
int bin = 0 ;
int pow = 0 ;
while(dec != 0){
int rem = dec % 2 ; // remainder when we are dividing of 2
bin = bin + rem * (int)Math.pow(10, pow) ;
dec = dec/2 ;
pow++ ;
}
return bin ;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
System.out.println(“Enter a decimal : “);
int dec = sc.nextInt() ;
System.out.println(“Binary of ” + dec + ” = ” + binary(dec));
}
}
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int remain = a;
String ans = “”;
while (remain > 0) {
ans += remain % 2;
remain = remain / 2;
}
System.out.println(new StringBuffer(ans).reverse());
sc.close();
import java.util.Scanner;
class Beginner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter your number:”);
int decimal = sc.nextInt();
int count=0;
int binary =0;
int rem;
int temp;
double res;
temp=decimal;
while(temp!=0)
{
rem = temp%2;
res = Math.pow(10,count);
binary = (int) (binary+rem*res);
temp/=2;
count++;
}
System.out.println(“Binary Number “+binary);
sc.close();
}
}
import java.util.Scanner;
class Beginner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print(“Enter your number:”);
int decimal = sc.nextInt();
int count=0;
int binary =0;
int rem;
int temp;
double res;
temp=decimal;
while(temp!=0)
{
rem = temp%2;
res = Math.pow(10,count);
binary = (int) (binary+rem*res);
temp/=10;
count++;
}
System.out.println(“Binary Number “+binary);
sc.close();
}
}
I think this code will also do the conversion.
public class decitobinary {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int deci = s.nextInt();
StringBuilder binary = new StringBuilder();
while(deci>1){
binary.append(deci%2);
deci/=2;
}
binary.append(deci);
binary.reverse();
System.out.println(binary);
}
}