How to convert decimal to binary in c++

 In this article, we will learn how to convert decimal to binary in c++ in detail. It is highly recommended to go through all the examples stated in the article and try the code yourself. We have designed different diagrams and code examples to make this problem a piece of cake for our readers. 

Why binary and its conversion is important?

We all know how crucial binary numbers and their conversions are in computer science. As we discussed in our previous post, the data is stored in binary form on all storage devices and computers. But this stored data is first converted to its original form before displaying it to the user. This article is also designed to learn how to convert decimal to binary in c++. Let's start our today's guide.

How to convert binary to decimal?

Let's see how can we convert decimal to binary stepwise. 

If we examine the above figure and try to convert it into a coding algorithm, it is basically a three-step process. Let's see what are these steps.

we need two int-type variables. Let's say the first variable is  binary_num initialized with zero to store remainder. The second one is variable digit_ location used to specify the location of bit in variable binary_num. It is initialized with 1 because initially, the bit location is unit.
  1. In the first step, divide the user-chosen number by 2. It will give the remainder 0  if the number is even and 1 if it is odd. 
  2.  Further, the remainder calculated in the first step is multiplied by variable digit_location and added into variable binary_num.
  3. The third step is a very simple but important part of the program. In this step, multiply 10 with the variable digit_location to increment it. By incrementing bit location, we mean to convert units to tens, tens to hundreds, etc. Check out the below figure to understand the purpose of variable bit_location.

The Above mentioned three steps will execute inside the body of for loop. Our loop condition is also dependent on the user input. The user-chosen number will be divided by 2 inside loop condition every time the loop performs an iteration. The three-step process will repeat until the user input number deci_num is greater than 0. Once it becomes less than 0, no further iteration will take place and the final result will be printed.
That's all about the logic and algorithm of the problem. Now let's see the code and understand the concept better.

#include <iostream>
using namespace std;
int main()
{
	//declaring variables
	int deci_num, Bit_location, j, binary_num = 0;
	cout<<"Convert Decimal to Binary"<<endl;
	cout << "------------------------" << endl;
	//taking input
	cout<<"Enter a decimal number"<<endl;
	cin>>deci_num;
	//loop to execute three step algo
	Bit_location = 1;
	for (j = deci_num; j > 0; j = j / 2)
	{
		//first and second step in one line
		binary_num = binary_num + (j % 2)*Bit_location;
		//third step
		Bit_location = Bit_location * 10;
	}
	//displaying result 
	cout<<"The Binary of "<< deci_num<<" is "<< binary_num <<endl;
	system("pause");
	return 0;
}


As you can see the above code is very simple and self-explanatory. Now see the output of the code below.




Here is the end of our article on how to convert decimal to binary in c++. Hope you enjoyed it and found it useful. Thanks for reading and supporting us.

Post a Comment

0 Comments