# How to Convert Decimal to Binary in C++

In this article, we are going to learn how to convert Decimal to Binary in C++. Here we are going to learn about two number systems one is binary and another is Decimal. Binary numbers are base 2 numbers and represented in 0s and 1s. Decimal numbers are base 10 numbers and are represented in numbers from 0 to 9.

### C++ Program to convert decimal to binary

In this, we will understand how to Convert Decimal to Binary in C++ with the below program. We have used some custom logic instead of a built-in function.

```#include<iostream>

using namespace std;

int main()
{
long long decimalNum, tmpDecimalNo;
long long  binNum;
int rem, place = 1;

binNum = 0;

cout <<"Please enter the decimal number: ";
cin >> decimalNum;

tmpDecimalNo = decimalNum;

while(tmpDecimalNo > 0)
{
rem = tmpDecimalNo % 2;

binNum = (rem * place) + binNum;

tmpDecimalNo = tmpDecimalNo/2;

place *= 10;
}

cout << "\nThe Binary equivalent of Decimal number" <<  decimalNum << " is " << binNum;

return 0;
}

```

Output

```Please enter the decimal number: 8

The Binary equivalent of Decimal number 8 is 1000

```

### C++ program to convert from Decimal to Binary

In this example, we are going to write logic to make higher range conversions. In above example,
we can convert numbers upto 18 binary bits.So for higher range numbers we can use below logic.

Note: In this code we are using the custom function strrevv() to reverse the binary string. But if you are running this code on windows platform then you can use strrev() function which is available in string.h.
strrev() is not available on Linux so we have to write our own custom strrevv().

```
#include <stdio.h>
#include <string.h>

char *strrevv(char *str)
{
char *ch1, *ch2;

if (! str || ! *str)
return str;

for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}

int main()
{
long decimalNum, tmpDecimalNo;
char binNum[65];
int index = 0;

cout << "Please enter the decimal number: ";
cin >> decimalNum;

tmpDecimalNo = decimalNum;

while(tmpDecimalNo > 0)
{
binNum[index] = (tmpDecimalNo % 2) + '0';

tmpDecimalNo =tmpDecimalNo / 2;

index++;
}

binNum[index] = '#include <stdio.h>
#include <string.h>
char *strrevv(char *str)
{
char *ch1, *ch2;
if (! str || ! *str)
return str;
for (ch1 = str, ch2 = str + strlen(str) - 1; ch2 > ch1; ++ch1, --ch2)
{
*ch1 ^= *ch2;
*ch2 ^= *ch1;
*ch1 ^= *ch2;
}
return str;
}
int main()
{
long decimalNum, tmpDecimalNo;
char binNum[65];
int index = 0;
cout << "Please enter the decimal number: ";
cin >> decimalNum;
tmpDecimalNo = decimalNum;
while(tmpDecimalNo > 0)
{
binNum[index] = (tmpDecimalNo % 2) + '0';
tmpDecimalNo =tmpDecimalNo / 2;
index++;
}
binNum[index] = '\0';
strrevv(binNum);
cout << "\nThe Binary equivalent of Decimal number " << decimalNum << " is " << binNum;
return 0;
}
';

strrevv(binNum);

cout << "\nThe Binary equivalent of Decimal number " << decimalNum << " is " << binNum;

return 0;
}
```

Output

```Please enter the decimal number: 10

The Binary equivalent of Decimal number 10 is 1010

```