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
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);
}
}