Answers for "decimal to binary using stack"

0

decimal to binary using stack

import java.util.*;

public class ConvertDecimalToBinaryUsingStack {
	public static void main(String args[]) {
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number: ");
		int num = input.nextInt();
		Stack stack = new Stack();

		if (num == 0) {
			System.out.println("BINARY is 0000");
		} else if (num == 1) {
			System.out.println("Binary is: 0001");
		} else {
			while (num != 0) {
				int a = num / 2;
				int b = num % 2;
				stack.push(b);
				num = a;
			}
		}

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

		System.out.println(" ");
	}
}
Posted by: Guest on September-06-2021

Code answers related to "decimal to binary using stack"

Browse Popular Code Answers by Language