Java Program to Convert a Decimal Number to a Binary Number using Arrays as Stacks
In Java, we can convert a decimal number to binary using the built-in java.util.Stack class. The Stack class provides the necessary operations, such as push() and pop(), to implement the stack data structure.
Approach:
- The inbuilt stack class is not used rather an array is used and push and pop methods are defined to perform the insertion and removal of the elements.
- The stack is implemented using the static array.
- The static variable count keeps track of the number of elements inserted.
- The remaining procedure is exactly similar to the previous example.
Example 1
In the above code, we have created a DecimalToBinaryStack class to implement a stack using an array. The push() method is used to add an item to the stack, the pop() method is used to remove an item from the stack, and the isEmpty() method checks if the stack is empty.
The decimalToBinary() method takes a decimal number as input and converts it to binary using the stack. It repeatedly divides the decimal number by 2 and pushes the remainder onto the stack. Finally, it pops the elements from the stack and prints them to obtain the binary representation. In the main() method, we take input from the user, call the decimalToBinary() method, and display the binary representation.
FileName : DecimalToBinaryStack.java
import java.util.Scanner; public class DecimalToBinaryStack { private static int[] stack; private static int top; private static int size; public DecimalToBinaryStack(int size) { stack = new int[size]; top = -1; this.size = size; } public void push(int item) { if (top == size - 1) { System.out.println("Stack is full. Cannot push element."); return; } stack[++top] = item; // Increment top and add item to the stack } public int pop() { if (top == -1) { System.out.println("Stack is empty. Cannot pop element."); return -1; } return stack[top--]; // Remove item from the stack and decrement top } public boolean isEmpty() { return top == -1; // Check if the stack is empty } public static void decimalToBinary(int decimalNumber) { DecimalToBinaryStack stack = new DecimalToBinaryStack(20); // Create an instance of the stack while (decimalNumber > 0) { int remainder = decimalNumber % 2; // Get the remainder when dividing by 2 stack.push(remainder); // Push the remainder to the stack decimalNumber /= 2; // Divide the decimal number by 2 } System.out.print("Binary representation: "); while (!stack.isEmpty()) { System.out.print(stack.pop()); // Pop and print elements from the stack } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a decimal number: "); int decimalNumber = scanner.nextInt(); decimalToBinary(decimalNumber); // Convert decimal to binary using the stack } }
Output
Enter a decimal number: 27 Binary representation: 11011
Complexity
The time complexity of the code is O(log N) because the decimalToBinary() function performs a loop that divides the decimal number by 2 in each iteration. Since the loop continues until the decimal number becomes 0, the number of iterations required is proportional to the logarithm (base 2) of the decimal number.
The space complexity of the code is O(1) because the stack size is fixed at 20. The space usage remains constant regardless of the input size. the stack size is fixed, so the space complexity can be considered constant O(1).
Example 2
In the above code, we use the java.util.Stack class to create a stack and store the binary digits. We input a decimal number from the user and enter a loop that performs the conversion.
FileName: DecimalToBinaryStack.java
import java.util.Scanner; import java.util.Stack; public class DecimalToBinaryStack { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a decimal number: "); int decimalNumber = scanner.nextInt(); Stack<Integer> stack = new Stack<>(); // Create a stack to store binary digits while (decimalNumber > 0) { int remainder = decimalNumber % 2; // Calculate the remainder when dividing by 2 stack.push(remainder); // Push the remainder onto the stack decimalNumber /= 2; // Divide the decimal number by 2 } System.out.print("Binary representation: "); while (!stack.isEmpty()) { System.out.print(stack.pop()); // Pop and print elements from the stack } } }
Output
Enter a decimal number: 22 Binary representation: 10110
Complexity
The time complexity of the code is O(log N), where N is the decimal number being converted. The logarithmic time complexity arises from the loop that repeatedly divides the decimal number by 2.
The space complexity of the code is also O(log N) because the stack stores the binary digits, and the number of digits in the binary representation is logarithmic in relation to the decimal number.