Last Updated: 26 Nov, 2020

Binary Confusion

Easy

Problem statement

In the event of confusion, Ninja and his friends were asked to solve an easy problem given by their teacher. However, even after taking several hours, they could not solve the problem.

A value of decimal number ‘N’ is given to them, and they are asked to convert it into its binary equivalent and return it as the answer. Since they are stuck for a while, they ask you to solve the problem. Can you help solve this problem?

Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. The test cases follow.

The first line of each test case contains a single integer ‘N’, given to Ninja and his friends.
Output Format:
For each test case, print the binary equivalent of the given number.

Print the output of each test case in a separate line.
Note:
You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10 ^ 3
0 <= N <= 10 ^ 6


Time Limit: 1 sec

Approaches

01 Approach

The steps are as follows:

  • First declare an empty string, say ‘binary’.
  • Now iterate till ‘n’ is greater than 0 with the help of iterator pointer ‘i’.
  • Check if n is even. If yes then push back “0” in the string ‘binary’.
  • If no then push back “1” in the string ‘binary’.
  • Now make n = n / 2.
  • Return ‘binary’ to the function.