In the previous article, we have discussed Java Program for Hexadecimal to Decimal

In this article we will discuss about how to convert Binary to Octal.

## Java Program to Convert Decimal to Binary

Before jumping into the program directly, let’s first know about decimal to binary.

**Decimal:**

Decimal number mainly defines the base of 10 in the number system. This number is basically consists of 10(ten) single digits like 0,1,2,3,4,5,6,7,8 and 9 with base 10. It is also known as a position value system.

Example: (183)10, (321)10

where, (number)10 number represents the original decimal number and 10 is the base.

**Binary:**

Binary number mainly consists of only two numbers i.e. 0 and 1. The base address of the binary number is 2. For low voltage signal the value will 0 and for the high voltage signal the value will 1.

Example: (1001)2, (111000)2

where, (number)10 number represents the original binary number and 2 is the base.

Let’s see different ways convert decimal to binary.

### Method-1: Java Program for Decimal to Binary By Using Integer.toBinaryString() method

In this method we have used `Integer.toBinaryString()`

method to convert decimal to binary.

Let’s see the program to understand it more clearly.

import java.util.*; public class DecimalToBinary { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int n1=sc.nextInt(); System.out.println(Integer.toBinaryString(n1)); } }

Output: Enter a decimal: 52 110100

### Methos-2: Java Program for Decimal to Binary By Using Custom Logic

In this approach we have used custom logic. Let’s see the program to know it’s actual implementation.

public class DecimalToBinary { public static void toBinary(int dec) { int binary[] = new int[100]; int ind = 0; while(dec > 0){ binary[ind++] = dec % 2; dec = dec/2; } for(int k = ind-1;k >= 0;k--) { System.out.print(binary[k]); } } public static void main(String args[]) { System.out.println("The Decimal number of 52 is: "); toBinary(52); System.out.println("\nThe Decimal number of 63 is: "); toBinary(63); System.out.println("\nThe Decimal number of 36 is: "); toBinary(36); } }

Output: The Decimal number of 52 is: 110100 The Decimal number of 63 is: 111111 The Decimal number of 36 is: 100100

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

**Related Java Programs:**