17.19 Lab 8B: printing a binary number
This lab asks you to use string formatting to print a binary number. First you will prompt the user for an integer, using exactly the prompt "Enter a decimal integer: " including the colon character and a space following it. Then print out the decimal number and it's binary equivalent. You do not need to do any mathematical conversion of the integer to binary, you can use the format method.
For example, if the prompt and input are
Enter a decimal integer: 10
The output would be
10 in binary is 1010
If the prompt and input are
Enter a decimal integer: 367
The output would be
367 in binary is 101101111
If you could help me write this code in Python that would be much awesome
#Reading a decimal number input from user num = eval(input("Enter a decimal integer: ")) inputNum = int(num) #Result is used to store binary value result = "" #Repeat the loop till n becomes negative while (num > 0): #Finding the reminder rem = num % 2 #Appending reminder to the result result += str(rem) #Making num to its half num = num // 2 #Reverse the value of result result = result[::-1] #Printing final binary value to screen print(inputNum,"in binary is",result)
Get Answers For Free
Most questions answered within 1 hours.