Java Program to Convert a Decimal Number to Binary Number using Stacks

This is a Java Program to find the binary equivalent of a decimal number using stacks. Stack is an area of memory that holds all local variables and parameters used by any function and remembers the order in which functions are called so that function returns occur correctly. ‘push’ operation is used to add an element to stack and ‘pop’ operation is used to remove an element from stack. ‘peek’ operation is also implemented returning the value of the top element without removing it. The relation between the push and pop operations is such that the stack is a Last-In-First-Out (LIFO) data structure.

For converting decimal number to binary, initially we push all the binary digits formed into the stack. After the entire number has been converted into the binary form, we pop one digit at a time from the stack and print it. Therefore we get the decimal number converted into its proper binary form.

Here is the source code of the Java program to convert decimal number to binary using stacks. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

/*
 *  Java Program to Convert a Decimal Number 
 *  to Binary Number using Stacks
 */
 
import java.util.*;
 
/*  DecimalToBinaryUsingStacks  */
public class DecimalToBinaryUsingStacks
{
    public static void main(String[] args) 
    {    
        Scanner scan = new Scanner(System.in);
        /* Creating Stack object  */
        Stack<Integer> stk = new Stack<Integer>();
        /* Accepting number */        
        System.out.println("Enter decimal number");
        int num = scan.nextInt();
 
        while (num != 0)
        {
            int d = num % 2;
            stk.push(d);
            num /= 2;
        }        
        /* Print Binary equivalent */
        System.out.print("\nBinary equivalent = ");
        while (!(stk.isEmpty() ))
        {
            System.out.print(stk.pop());
        }
        System.out.println();
    }
}
Enter decimal number
12345
 
Binary equivalent = 11000000111001
 
 
Enter decimal number
99
 
Binary equivalent = 1100011
 
 
Enter decimal number
24162
 
Binary equivalent = 101111001100010
 
 
Enter decimal number
347562318
 
Binary equivalent = 10100101101110110000101001110

Related posts:

New Features in Java 15
Guide to Spring Cloud Kubernetes
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Implement Repeated Squaring Algorithm
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Skew Heap
So sánh Array và ArrayList trong Java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Spring Security OAuth2 – Simple Token Revocation
Java Program to Solve Knapsack Problem Using Dynamic Programming
Introduction to Project Reactor Bus
Java Program to Check Multiplicability of Two Matrices
Reactive WebSockets with Spring 5
Prevent Brute Force Authentication Attempts with Spring Security
Receive email using POP3
Java Program to Implement Strassen Algorithm
A Guide to Spring Cloud Netflix – Hystrix
Java Program to implement Bit Set
An Intro to Spring Cloud Contract
Java Program to Implement Selection Sort
Java Program to Find Basis and Dimension of a Matrix
Java Program to Encode a Message Using Playfair Cipher
Chuyển đổi từ HashMap sang ArrayList
Logging in Spring Boot
Java Program to Perform Complex Number Multiplication
Using Optional with Jackson
Java Program to Find the Minimum value of Binary Search Tree
An Example of Load Balancing with Zuul and Eureka
Database Migrations with Flyway
Java Program to Implement Dijkstra’s Algorithm using Set
Java Program to Generate All Possible Combinations of a Given List of Numbers