convert decimal to binary c code
4 stars based on
Before we get too technical, let's start with the following joke: There are 10 types of people, those who understand binary, and those who don't.
For those of you who didn't understand the joke, please keep reading. I am sure it will make sense to you before this article comes to a close. First, let's start with a little background on the topic of decimal and binary numbers. We are all familiar with the decimal number system Also known as the base ten number system. Every integer can be represented in the decimal number system decimal to binary conversion function in c some combination of the numbers from 0 to 9.
On the other hand we can also represent all integers decimal to binary conversion function in c the binary number system. Therefore if we are going to represent integers in binary, we can only use the numbers 1 and 0.
As a result the decimal number 2 is represented as 10 in the binary number system Now that you have made it to this point in the article, the joke in the beginning of this post should start to make sense.
Before we start writing code, let's develop a plan of attack. For our attack we will be using an algorithm that decimal to binary conversion function in c our decimal number by two. Each time we divide our decimal number in two, we will save the remainder in a list.
Then we will perform the division on the remaining decimal number, until our remaining decimal number equals 0. If that was a little unclear, don't worry, this will make more sense with the following example:. Let's start with the number First we will divide our number by two, then stick the remainder in the front of a list. Since this is our first time, our remainder will be the only item in the list.
Thus our list looks like this: Next we we will take the 11 from the first division, then once again divide by two, and stick the remainder in the front of our list. Our list now looks like this [ 1, 1 ]. Now lets continue the same process with the left over 5 from the last step.
Adding decimal to binary conversion function in c remainder to the front of the list, we now get [ 1, 1, 1 ]. Now we perform the operation again on the remaining 2. Now we add the remainder to the front of the list. Our list now looks like this [ 01, 1, 1 ]. The marks the end of the process. When we get to 0 Remainder 1we add the remainder to the front of the list, and then we are done with the conversion. So adding our remainder of 1 to the front of the list we get [ 1, 0, 1, 1, 1 ].
Now, removing the commas and the brackets, our list is Therefore, the decimal number 23 is when converted to decimal to binary conversion function in c. Let's try this again real quick with the number 13, but without all the dialog. Thus, 13 in decimal has the form in binary. Now that we understand how to solve our problem, let's write some code that will perform this conversion for us for any positive integer that we give it. I will explain how it is implementing the decimal to binary conversion algorithm below.
This section tells the preprocessor, that we will decimal to binary conversion function in c needing to use the iostream and list libraries.
If we didn't include these libraries, we wouldn't be able to use the "cout" or the "list" functionality that is written in our code. Including these libraries at the top, is a lot like adding all the code from the iostream and list libraries to our file, but without the clutter.
Next, we specify that our decimal to binary conversion function in c is using the standard namespace using namespace std. If we did not include this, we woud need to replace all or our cout decimal to binary conversion function in c list statements with std:: The line void binaryConvert int input ; is a function prototype. It it a lot like saying, I will decimal to binary conversion function in c using the following function in my code, but I am not going to define it until after I use it.
If we call the function before decimal to binary conversion function in c it, we will get an error when compiling unless we include the prototype here first. Additionally, void says that the function does not return a value. I just as well could have called it "CrabNebula" if I wanted, but that wouldn't have made any sense so "binaryConvert" it is.
Then inside the parentheses, int input says that the function will be given an integer when it is called, and to put the integer in a variable that is named "input". You can think of a variable as a box that holds stuff for your program to use later. The next part is the main function. It tells the program where to start. The int here says that the main function will return an integer value to the operating system when it decimal to binary conversion function in c done.
More specifically, the for loop is going to create an integer variable named "i" and initially give it a value of 0 Think of a zero being placed in the "i" box. Then as long as the value stored in "i" is less than 17, repeat the loop. Thus, we are setting up a loop that will repeat with the values 1,2,3, Next, we have the instructions for the loop.
This explains exactly what will happen each time the loop runs. The next line, binaryConvert i ; tells our program to call the "binaryConvert" function, Which has not yet been defined and provide it with the value that is stored inside of "i". Now the only thing remaining is to tell our program what the "binaryConvert" function does. Once again, as I described above in the prototype, void binaryConvert int input says we are defining a function named "binaryConvert". The function will not return a value, and the function needs to be provided an integer value, which is to be stored in a variable named "input".
Next, we declare a list to hold our binary number. Additionally, we are naming this list "binary". Then, int rem; creates a new integer variable called "rem". The "rem" variable is where we will be temporarily storing our remainders. A do-while loop tells our program to do the instructions at least one time, but decimal to binary conversion function in c continue doing these instructions as long as the while argument is true. In our case our while argument is input! This tells the program to continue repeating the instructions in the do section as long as the value in the input variable is not equal to 0.
This is the dividing by 2 algorithm that I presented earlier in the article. When "input" equals zero, we are essentially at the 0 Remainder 1 step of the algorithm. Ok, now we just need to discuss what is happening inside of this do-while loop. Then the code binary.
This is exactly what we were doing in our example towards the top of this article. Once again, as previously mentioned while input! Another way to think about this is that while input! You can think of an iterator as an arrow that marks the item that we are looking at the list. Then as long as our iterator named "it" is not marking the end of our list, go ahead and repeat the instructions inside of the for loop. This code says to print out the value of the item in our list that is currently being marked by our iterator.
So essentially, what this for loop is doing, is printing out the digits of the remainders that we added to the front of our "binary" list. This is the binary representation of our decimal number passed into the "binaryConvert" function! That way, each time our "binaryConvert" function is called, it prints on a new line. Ok now the code has been explained. Let's give it a test run.
Here I have a Bash terminal open and typing the ls command shows me that my program convert. This command says to compile the file named "convert. The command causes our prompt to drop down to the next line without any errors.
Therefore, compiling is successful! Now if we run the ls command one more time we can see our new executable named "convert" displayed in red below.
Now let's run the executable by entering. On the left hand side we can see the numbers from 0 through 16, and on the right hand side we can see the binary representation of each of these numbers.
If you enjoyed this article, please share, and come back for more great content! I'll be back soon with more awesome, math, science and programming content! Great article, though I was wondering if you might be able to help me It seems that some words are missing, for some reason they are blank!
I have been noticing this more and more across Steemit I am using google Chrome on Ubuntu if that helps.
If that was a little unclear, don't worry, this will make more sense with the following example: Adding this remainder to the front of the list, we now get [ 1, 1, 1 ] Now we perform the operation again on the remaining 2. Also I have include the code in text form here for your convenience. Now let's look at the main function. Finally, the program exits and returns a value of 0 to the operating system.