Answers for "transforming a decimal number into a binary number by using stack in java without using predefined 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 "transforming a decimal number into a binary number by using stack in java without using predefined stack"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language