
Below is a screenshot of how this is done on paper. Where n starts at 0 (right most) and goes up by one, as we go over the binary digits from right to left. The function will traverse over the string from right to left and multiple the digits by 21. Our implementation of this function will assume that the parameter "num” is coming in as a string object. This function should take in a parameter named "num" similar to the first function. Write another function and name it "binaryToDecimal”. After the while loop return the variable name "bits”. When dividing by 2 we don't want decimal numbers so use the double division sign"//" also known as the floor division. Update the variable name “num” by diving it by 2.


The reason why we add to the front and not the end, is because we read binary right to left. Cast the remainder to a string object and add it to the front of the variable name “bits”. This can be accomplished with the modules operator. Inside the while loop you have to do two things: Calculate the remainder of “num" divided by two. Write a while loop with the conditional “num > 0". Inside the method create a variable named "bits” and assign an empty string to it. Refer back to the slides for more information, but basically we are diving by 2 and storing the remainder.

Below is an example of how to convert a decimal number to a binary number. We will implement it without a stack and simply use a string object for the representation a binary number. There are many ways to implement this function. This function will take in a decimal number and return it converted to binary form. Define a function and name it “decimalToBinary”. In the comment section on top, include your name, todays date and program name (Lab 14, BinaryDecimalConverter.py). To get started create a new file and save it as BinaryDecimalConverter.py. The second function named “binaryToDecimal”, which will take in a binary number as a string and return the number in decimal form. The first function named “decimalToBinary”, which will take in a number in decimal format and return the number in binary form. Binary-Decimal Converter For this programming task you will write two functions. Once inside the winpython folder double click on "Spyder” icon and it will open up the Spyder IDE.

If you need help getting to the folder please refer back to lab 1. Navigate to the winpython folder on your computer. For this lab, create another folder, called lab14.Write code to convert a decimal number to binary and vice versa Preliminaries
