Part 4 of 28 in Maths - Beginner set 01  

Convert from decimal to binary by admin

Problem statement

Given a number n, convert the number to its binary representation.

Input

First line of input will contain T = number of test case. Next T lines will each have a number n. n will be between 0 and 1000000000 (10^9)

Output

For each number print its binary form on a single line. There should be no leading zeroes in the result.

Sample Input
4
5
12
25
36
Sample Output
101
1100
11001
100100
1) Are you good for n = 0 ?

As we know, decimal number system uses 0-9 digits to represent a number. The base used is 10.

Similarly, in binary number system, any number is represented using only 2 digits, 0 and 1.

For example: Consider the number 12

12 = 1 * 10^1 + 2 * 10^2

is written as 12 in decimal form.

Now,
12 = 1 * 2^3 + 1 * 2^3 + 0 * 2^1 + 0 * 2^0

can be written in binary system as 1100.

Any number in any base can be converted to be represented in another base. The actual value of the number does not change.

Steps for conversion from decimal to binary:
  1. If the number to be converted is 0, return 0 as a string.
  2. Repeatedly divide the number by 2 till it becomes 0.
  3. While dividing, go on storing the remainder (the result of num % 2) before each division in a character array (or a string).
  4. Reverse the string obtained after the number turns 0.
  5. The final string is the representation of the given number in binary form. Return it.

Example of Decimal to Binary Conversion:

29 / 2 = 14 Remainder = 1
14 / 2 = 7  Remainder = 0
 7 / 2 = 3  Remainder = 1
 3 / 2 = 1  Remainder = 1
 1 / 2 = 0  Remainder = 1

Reversing the order of remainders, gives us 11101, which is the binary notation of decimal number 29.

Note: The same steps can be used for conversion of decimal number to any base, say K. Instead of continuously dividing the number by 2, we need to divide by K.

editorial written by shivshnkr

#include <iostream>
#include <algorithm>

using namespace std;

/* function returns pointer to character array
   that stores the number in binary */
char * decimal_to_binary(int num)
{

    char binary_string[101]; // array to hold binary string
    int counter = 0; // variable to store counter
    int remainder; // variable stores current remainder

    /* handle for 0 separately */
    if (num == 0)
    {
        /* store binary number 0 in char array */
        binary_string[0] = '0';
        binary_string[1] = '\0';
        return binary_string;
    }

    /* a loop that goes on dividing the number till
       till it becomes equal to 0 */

    while (num != 0)
    {

        /* value of remainder when divided by 2 */
        remainder = num % 2;

        /* number is continuously divided by 2 */
        num /= 2;

        /* binary string at index counter is assigned the
           current remainder in character format */
        binary_string[counter] = remainder + '0';

        /* increment counter */
        counter++;
    }

    /* we write NULL to the end of the string */
    binary_string[counter] = '\0';

    /* reverse the binary string and return */
    reverse (binary_string, binary_string + counter);
    return binary_string;
}
int main()
{
    int testcases;
    cin >> testcases;
    while (testcases--)
    {
        int number; // variable to store number

        cin >> number; //read input from stdin

        /* print output to stdout */
        cout << decimal_to_binary(number) << endl;
    }
    return 0;
}


featured solution by shivshnkr



To try out your code



Sign in

Sign up