Java Program to Convert a Decimal Number to Binary Number using Stacks Last Updated : 17 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Java is high level, compiled as well as interpreted programming language. Stack is an abstract data type used in most of the programming languages and can be implemented using arrays or linked list. Stack data structure follows the principle of LIFO (Last In First Out) . Stack allows push, pop, peek operations to be performed. The push operation is used to insert a new element into the stack. The pop operation is used to remove an element from the top of the stack. The peek operation is used to get the top element of the stack without removing it from the stack. A decimal number can be converted into binary number using the push and pop operation of the Stack. Now, Java provides inbuilt Stack class which can be used to suit our purpose. Converting a decimal number to binary number using stacks: Using predefined stack.Using array as a stack. Method 1: Using Predefined Stack Approach: A predefined stack class is used to create a stack. The user is asked to enter a decimal number. Next, a while loop is executed where the mod result of the number by 2 is pushed into the stack and the number is divided by 2. This is repeated till the number is greater than 0. When the number is 0 the while loop is terminated and another while loop is starts to print the binary equivalent of the decimal number. The remainders stored in the stack are popped in LIFO which gives the desired binary representation. Code Implementation: Java // Java Program to Convert a Decimal Number // to Binary Number using Stacks import java.util.*; public class DecimalToBinary { public static void main(String args[]) { // creating a stack Stack<Integer> stack = new Stack<Integer>(); System.out.println("Enter a decimal number: "); int num = 23; while(num>0) { int r = num%2; // pushing the remainder inside the stack stack.push(r); num/=2; } System.out.print("Binary equivalent: "); while (!(stack.isEmpty() )) { // printing the resultant binary number stored in the // stack in LIFO manner System.out.print(stack.pop()); } } } OutputEnter a decimal number: Binary equivalent: 10111 The time complexity is O(log n), where n is the decimal number. The auxiliary space is O(log n) as well. Method 2: Using Array as a stack Approach: The inbuilt stack class is not used rather an array is used and push and pop methods are defined to perform insertion and removal of the elements.The stack is implemented using the static array. The static variable count keeps track of the number of the elements inserted. The remaining procedure is exactly similar to the previous example. Java // Java Program to Convert a Decimal Number // to Binary Number using Stacks import java.util.*; public class DecimalToBinary { static int arr[] = new int[1000]; // maintaining count variable as the top // of the stack static int count; // push at the count index and increment the count public static void push(int n) { arr[count++] = n; } // pop all the elements starting from count-1 // till 0 public static void pop() { for (int i = count - 1; i >= 0; i--) { System.out.print(arr[i]); } } public static void main(String args[]) { System.out.println("Enter a decimal number: "); int num = 46; while (num > 0) { int r = num % 2; push(r); num /= 2; } System.out.print("Binary equivalent: "); pop(); } } OutputEnter a decimal number: Binary equivalent: 101110 Time complexity: O(logn) as here while loop runs logn times Auxiliary space: O(1) Comment More infoAdvertise with us Next Article Java Program to Convert a Decimal Number to Binary Number using Stacks S Shreyasi_Chakraborty Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Convert a Decimal Number to Binary Number using Arrays as Stacks Given an Integer number convert into Binary Number using arrays as a stack. Example: Input : 10 Output: 1010 Input : 16 Output: 10000 Approach: Divide the number by 2 and store the remainder of the number in the array.Divide the number by 2.Repeat the process until the number becomes zero.Print the 1 min read Java Program to Convert a Decimal Number to Binary & Count the Number of 1s As per the number system, default computations are carried over decimal numbers whose base is standardized as 10. Machine computes all the execution at the physical layer in 0s and 1s. So arises a need for a number system with base 2 known as a binary number system. A binary number can be converted 4 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Java Program to Convert Binary to Hexadecimal The Hexadecimal number system as the name suggests comprises 16 entities. These 16 entities consist of 10 digits, 0-9 representing the first 10 numbers of the hexadecimal system as well. For the remaining 6 numbers, we use English alphabets ranging from A through F to represent the numbers 10 to 15. 6 min read Java Program to Convert Octal to Decimal Octal numbers are numbers with 8 bases and use digits from 0-7. This system is a base 8 number system. The decimal numbers are the numbers with 10 as their base and use digits from 0-9 to represent the decimal number. They also require dots to represent decimal fractions.We have to convert a number 3 min read Java Program to Convert Binary to Octal A Binary (base 2) number is given, and our task is to convert it into an Octal (base 8) number. There are different ways to convert binary to decimal, which we will discuss in this article.Example of Binary to Octal Conversion:Input : 100100Output: 44Input : 1100001Output : 141A Binary Number System 5 min read Java Program to Convert Octal to Binary Given an Octal number as input, the task is to convert that number into its Binary equivalent number. Example: Input: Octal Number = 513 Output: Binary equivalent value is: 101001011 Explanation : Binary equivalent value of 5: 101 Binary equivalent value of 1: 001 Binary equivalent value of 3: 011Oc 5 min read Java Program to Convert Binary Code Into Equivalent Gray Code Using Recursion Convert the Binary code of the Number into it's equivalent Gray's code using recursion. Binary is the default way to store numbers, but in many applications, binary numbers are not useful and a variation of binary is needed. Gray code has the property that two successive numbers differ in only one b 3 min read Java Program to Convert Integer Values into Binary Given an integer in Java, your task is to write a Java program to convert this given integer into a binary number. Example: Input: = 45 Output: = 101101 Input: = 32 Output: = 100000 Integers: Integers are numbers whose base value is 10. The Integer or int data type is a 32-bit signed twoâs complemen 4 min read Java Program to Convert Byte Array to Long A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Given an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo 3 min read Like