# Decimal to Binary Conversion using Java

## Java Program to convert Decimal to Binary

Objectives :

• Decimal to Binary Conversion
• Converting Decimal to Binary
• Converting Decimal Number to Binary format
• Write a program to convert decimal to binary
• Write a program to convert decimal number to binary format
• Write a program to convert positive decimal number to binary format
• Write a program to convert decimal to binary using Scanner class
• Write a program to convert decimal to binary, take input using Scanner class

Java Program to convert Decimal to Binary :

[sourcecode lang=”java”]
public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[25];
int n = 0;
// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}
// Print Binary number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args){
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(14);
}
}
[/sourcecode]

Output :

1110

Java Program to convert Decimal to Binary using Scanner class :

[sourcecode lang=”java”]

import java.util.Scanner;

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[14];
int n = 0;

// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}

// Print Binary Number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num= scanner.nextInt();
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(num);

}
}

[/sourcecode]

Output :

Enter a number: 14

1110