Sail E0 Webinar
Question
Using stacks, how to obtain the binary representation of the number?
Options:
A .  public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num / 2;stack.push(digit);    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }
B .  public void convertBinary(int num){    Stack stack = new Stack();    while (num != 0)    {        int digit = num % 2;        stack.push(digit);    }     System.out.print("\nBinary representation is:");    while (!(stack.isEmpty() ))    {        System.out.print(stack.pop());    } }
C .  public void convertBinary(int num){    Stack stack = new Stack();    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());    } }
D .  None of the above
Answer: Option C


Here instead of adding the digits to an array, you push it into a stack and while printing, pop it from the stack.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers