Problem Statement
Given an integer value, convert the value into a binary string.
Input
The first line of input contains an integer value n
, where n>=0
.
Output
Print the binary representation of the given integer.
As is
The boilerplate code is already there to read the input from user as an integer into a variable, in the main()
function. Also it calls the convert_int_to_binary()
function, and prints the returned value. Do not edit this code.
To do ✍
convert_int_to_binary()
has one parameter: num
. Write code inside the function body to compute the binary representation of the given integer value, and return the same as a string.
Example 1
Test input
52
Test output
110100
Explanation
53 = 1*25 + 1*24 + 0*23 + 1*22 + 0*21 + 0*20
The binary string is the coefficients of the 2i
= 110100
Useful References
Python Program
# Complete the following function
# Return the binary string
def convert_int_to_binary(n):
# Boilerplate code - Do not edit the following
def main():
n = int(input())
print(convert_int_to_binary(n))
if __name__ == "__main__":
main()
Test case 1
0
0
Test case 2
100
1100100
Test case 3
54
110110
Test case 4
87
1010111
# Complete the following function
# Return the binary string
def convert_int_to_binary(n):
# bin() built-in function returns binary string of given int
binary_string = bin(n)
# But the binary string has a prefix of 0b
# Well! Remove that
binary_string = binary_string[2:]
return binary_string
# Boilerplate code - Do not edit the following
def main():
n = int(input())
print(convert_int_to_binary(n))
if __name__ == "__main__":
main()