-
Make variables called:
original
and set its value to the input number given by the end user.number
and set its value tooriginal
(so we can useoriginal
later)bit_value
and set its value to 1. Find the smallest bit value which is larger than thenumber
by doubling value ofbit_value
while it is smaller than or equal tonumber
.cards
is a string variable and stores the binary cards needed (‘B’ for black cards and ‘W’ for white cards).
-
Set the variable
bit_value
to 1 and find the smallestbit_value
which is larger thannumber
by multiplyingbit_value
by 2 while it is smaller than or equal tonumber
. You can do this by using a while loop. -
Now divide the
bit_value
by 2 and check ifnumber
is greater than or equal tobit_value
. If it is, add ‘W’ to string variablecards
and subtractbit_value
from thenumber
. If not, add ‘B’ to string variablecards
. Repeat whilebit_value
is greater than 1. Display the value ofcards
as the output. -
Test your program with some values on the boundaries (for example test it with numbers 255 and 256).