In this question we are given a number and asked to print its Binary Equivalent.
eg. 5 = 101, 2 = 10, etc.
we can solve this using the method we use to find binary equivalent of any number on paper-pen, that is using division by 2 and noting down the reminder.
now how to do it in computer code,
Firstly create a StringBuilder in which we are going to store our binary equivalent,
then we need a loop what will run till our given number is bigger the zero and do the following actions in the loop:
- Find the reminder using modulo operator.
- Append that reminder in the StringBuilder.
- Divide the given number by 2 and store the answer in the same variable.
Do the above steps until the number becomes zero.
In the end reverse the StringBuilder object and convert it in the String using the inbuild function.