Java Program to Convert Decimal to Binary - The Coding Shala

Home >> Java Programs >> Convert Decimal to Binary

 Hey there, welcome back to another post. In this post, we will learn how to convert decimal to binary in Java.

Java Program to Convert Decimal to Binary

You have given a decimal number. Write a Java program to convert decimal to binary.

Example 1: 

Input: 7
Output: 111

Input: 10
Output: 1010

Decimal to Binary Conversion using Array

Approach

To convert decimal to binary we can follow the below steps:

  • Store the remainder in the array when we divide the number by 2.
  • Divide the number by 2.
  • Follow the above two steps until the number is above 0.
  • Print the array in reverse order.

For example: 

  4 % 2 = 0 => 4/2 = 2
  2 % 2 = 0 => 2/2 = 1
  1 % 2 = 1 => 1/2 = 0

Binary of 4 is: 100

Java Program: 

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static void printBinary(int num) {
        List<Integer> arr = new ArrayList<>();

        while (num > 0) {
            int rem = num % 2;
            arr.add(rem);
            num = num / 2;
        }

        // print arr is reverse order
        for (int i = arr.size()-1; i >= 0; i--) {
            System.out.print(arr.get(i));
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a decimal number");
        int num = sc.nextInt();

        System.out.println("The binary of " + num + " is: ");
        printBinary(num);
    }
}

Output: 

Enter a decimal number
10
The binary of 10 is: 
1010

Decimal to Binary Conversion using Stack

Approach

Instead of using the array, we can use the Stack to store the reminder and then print it. Stack has the property that the first inserted element will come out last so we got all the elements in the reverse order.

Java Program: 

import java.util.Scanner;
import java.util.Stack;

/**
 * https://www.thecodingshala.com/
 */

public class Main {

    public static void printBinary(int num) {
        Stack<Integer> stack = new Stack<>();

        while (num > 0) {
            int rem = num % 2;
            stack.push(rem);
            num = num / 2;
        }

        // print stack
        while (!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a decimal number");
        int num = sc.nextInt();

        System.out.println("The binary of " + num + " is: ");
        printBinary(num);
    }
}

Output: 

Enter a decimal number
10
The binary of 10 is: 
1010

Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala