## Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!

### How to convert Decimal number to Binary number and vice versa in Java ?.

Program to implement how to convert Decimal number to Binary number and vice versa in Java.

```package com.hubberspot.convertors;

public class DecimalToBinary {

public static void main(String[] args) {

// Create a decimal number
int decimalNumber = 345;

// In order to convert a number from decimal
// to binary we use Integer class static method
// toBinaryString() , it returns back a string
// representation of binary value of the parameter
// passed to it
String binary = Integer.toBinaryString(decimalNumber);

// printing out the converted value on the console
System.out.println("Binary value of the " + decimalNumber + " is : "
+ binary);

// In order to convert binary number back to decimal number
// we use Integer class static method by name parseInt()
// it takes a string parameter which is binary string and
// second parameter as the radix which is decimal format
int originalDecimalNumber =  Integer.parseInt(binary, 2);

// printing out the binary and decimal number
System.out.println("Decimal value of the " + binary + " is : "
+ originalDecimalNumber);

}

}

```

Output of the program :

© 2021 Learn Java by Examples Template by Hubberspot