Problem F
Decimal to Binary
Write a Python function, decimal_to_binary(decimal), that takes as input an integer and returns a string with its binary representation.
How do we get a binary string from an integer? The easiest
method uses integer division by
-
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder . -
divided by gives the quotient and remainder .
Stop when reaching a quotient of
You should implement the above algorithm using a loop and
string methods.
Note that we are testing your code differently in this task, please only submit your function definitions, without any code outside the functions! The main python file, which handles input and output, is already provided. You can download and place the main file in the same directory as your python file. You can then run the main python file we provide to try out the samples.
Input
The input consists of one integer
Output
The output should consist of one string, the binary
representation of
Sample Input 1 | Sample Output 1 |
---|---|
8 |
1000 |
Sample Input 2 | Sample Output 2 |
---|---|
123 |
1111011 |
Sample Input 3 | Sample Output 3 |
---|---|
0 |
0 |