Question: Converting Decimal to Binary: Consider the following pseudocode Read (number) Loop (number > 0) 1) digit = number modulo 2 2) print (digit) 3) number
Converting Decimal to Binary: Consider the following pseudocode
- Read (number)
- Loop (number > 0)
1) digit = number modulo 2
2) print (digit)
3) number = number / 2
// from Data Structures by Gilbert and Forouzan
The problem with this code is that it will print the binary
number backwards. (ex: 19 becomes 11001000 instead of 00010011. )
To remedy this problem, instead of printing the digit right away, we can
push it onto the stack. Then after the number is done being converted, we
pop the digit out of the stack and print it.
So implementing this with Stack class in programming, and test your result.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
