Using stacks, how to obtain the binary representation of the number?
A.
B.
C.
D.
A.
Code:
public void convertBinary(int num)
{
Stack<Integer> stack = new Stack<Integer>();
while (num != 0)
{
int digit = num / 2;
stack.push(digit);
num = num % 2;
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
}
Code:
public void convertBinary(int num)
{
Stack<Integer> stack = new Stack<Integer>();
while (num != 0)
{
int digit = num % 2;
stack.push(digit);
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
}
Code:
public void convertBinary(int num)
{
Stack<Integer> stack = new Stack<Integer>();
while (num != 0)
{
int digit = num % 2;
stack.push(digit);
num = num / 2;
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
}
Code:
public void convertBinary(int num)
{
Stack<Integer> stack = new Stack<Integer>();
while (num != 0)
{
int digit = num % 2;
stack.push(digit%2);
num = num / 2;
}
System.out.print("\nBinary representation is:");
while (!(stack.isEmpty() ))
{
System.out.print(stack.pop());
}
}