There are four popular number systems used in a programming world. They are Binary, Octal, Hexadecimal and Decimal(0 to 9) number systems. Let us find Binary, Octal and Hexadecimal equivalents of a Decimal number using a simple conversion process as demonstrated in this article. You can move the code to a user-defined Function or Method from the MAIN method for reusability.

Remember that we are converting only **Integers** to other Number systems here. We use Division and Modulo Division **arithmetic operators** mainly to get the desired output.

## Convert a Decimal Number to Binary Number in Java

Binary number system uses only **two numbers Zero(0) and One (1)**. To convert a decimal number to binary, we have to divide the number by 2. Write the remainder aside. Now take the quotient as the main number and divide by 2. By writing all remainders aside, the final Binary number will be formed. The process should be continued until the quotient is less than 2. It contains only 0s and 1s.

Below **Java Program** converts and prints a **Decimal number to a Binary Number**.

public class DecimalToBinary { public static void main(String[] args) { // TODO Auto-generated method stub int num = 14, tmp; tmp = num; //int numberSystem = 2; String binary = ""; while(num >= 2) { binary = num%2 + binary ; num = num /2; } binary = num + binary ; System.out.println("Binary of " + tmp + " is " + binary); } } //OUTPUT //Binary of 14 is 1110

## Convert a Decimal Number to Octal Number in Java

Octal number system uses **8 numbers from 0 to 7**. To convert a decimal number to Octal, we have to divide the number by 8. Write the remainder aside. Now take the quotient as the main number and divide by 8. By writing all remainders aside, the final Octal number will be formed. The process should be continued until the quotient is less than 8.

Below program converts and prints a **Decimal number to an Octal number**.

public class DecimalToOctal { public static void main(String[] args) { // TODO Auto-generated method stub int num = 14, tmp; tmp = num; //int numberSystem = 8; String octal = ""; while(num >= 8) { octal = num%8 + octal; num = num /8; } octal = num + octal ; System.out.println("Octal of " + tmp + " is " + octal); } } //OUTPUT //Octal of 14 is 16

## Convert a Decimal Number to Hexadecimal Number in Java

Hexadecimal number system uses **16 numbers from 0 to 9 and A to F**. We can use **Lowercase or Uppercase** letters to represent a hexadecimal number from A to F (a, b, c, d, e, f). To convert a decimal number to Hexadecimal, we have to divide the number by 16. Write the remainder aside. Now take the quotient as the main number and divide by 16. By writing all remainders aside, the final Hexadecimal number will be formed. The process should be continued until the quotient is less than 16.

Below program converts and prints a **Decimal number to Hexadecimal Number**.

public class DecimalToHexa { public static void main(String[] args) { // TODO Auto-generated method stub int num = 26, tmp; tmp = num; //int numberSystem = 16; String hexa = ""; while(num >= 16) { hexa = getHexa(num%16) + hexa; num = num /16; } hexa = getHexa(num) + hexa ; System.out.println("Hexadecimal of " + tmp + " is " + hexa); } static String getHexa(int n) { String[] nums = {"0", "1","2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C","D", "E", "F"}; return nums[n]; } } //OUTPUT //Hexadecimal of 26 is 1A

The method getHexa() uses an **array** of Hexadecimal numbers in string notation for easy printing.

This is how we can convert a Decimal number to its equivalent Binary, Octal and Hexadecimal numbers in Java.

Share this article with your friends and colleagues to encourage authors.