Conversion Of Decimal to Binary Using Java
Java Code to Convert Decimal to Binary
This program helps to convert the given Decimal number to Binary
import java.util.Scanner;
public class DecimalToBinary {
public void printBinaryFormat(int number){
int binary[] = new int[number];
int index = 0;
while(number > 0){
binary[index++] = number%2;
number = number/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
public static void main(String a[]){
int n;
System.out.println("Enter a number to print decimal to binary");
Scanner in = new Scanner(System.in);
n = in.nextInt();
DecimalToBinary dtb = new DecimalToBinary();
dtb.printBinaryFormat(n);
}
}
Enter a number to print decimal to binary
5
101
Java Code to Convert Decimal to Binary
This program helps to convert the given Decimal number to Binary
Program:
public class DecimalToBinary {
public void printBinaryFormat(int number){
int binary[] = new int[number];
int index = 0;
while(number > 0){
binary[index++] = number%2;
number = number/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
public static void main(String a[]){
int n;
System.out.println("Enter a number to print decimal to binary");
Scanner in = new Scanner(System.in);
n = in.nextInt();
DecimalToBinary dtb = new DecimalToBinary();
dtb.printBinaryFormat(n);
}
}
Output:
Enter a number to print decimal to binary
5
101