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:

Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Implement Binary Tree
More Jackson Annotations
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Truyền giá trị và tham chiếu trong java
Getting the Size of an Iterable in Java
JPA/Hibernate Persistence Context
Java Program to Solve any Linear Equations
Converting a Stack Trace to a String in Java
HttpClient 4 Cookbook
Xử lý ngoại lệ trong Java (Exception Handling)
Một số ký tự đặc biệt trong Java
Java Program to implement Bi Directional Map
Java Program to Implement Self organizing List
A Guide to the Java ExecutorService
Lớp Collections trong Java (Collections Utility Class)
Write/Read cookies using HTTP and Read a file from the internet
Java Program to Implement LinkedHashMap API
Biến trong java
Check If a File or Directory Exists in Java
Java Program to Implement Shunting Yard Algorithm
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
How to Delay Code Execution in Java
Configuring a DataSource Programmatically in Spring Boot
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Generic Constructors in Java
Java Program to Implement CopyOnWriteArrayList API
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Transaction Propagation and Isolation in Spring @Transactional
Guava CharMatcher
Java Program for Douglas-Peucker Algorithm Implementation