Question: A stack can be used to convert decimal numbers to binary numbers. You are to write the method displayBinary that will convert a decimal number

A stack can be used to convert decimal numbers to binary numbers.


You are to write the method displayBinary that will convert a decimal number into a binary number using a stack.


The pseudocode for an algorithm that converts a decimal number to a binary number is given below.
public static void displayBinary (int decimalNum){
Stack stck=new Stack();
while decimalNum not equal to 0{
find remainder when decimalNum is divided by 2
push remainder on the stack
divide the decimalNum by 2
}
while stack isn't empty
pop num off the stack and display
}
Use the following header in writing your method displayBinary.
/**
Display the binary representation of decimalNum.
Precondition: decimalNum>0
*/
public static void displayBinary (int decimalNum)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

import javautil DecimalToBinaryUsingStacks public class DecimalToBinar... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!