## C++ || Snippet – How To Convert A Decimal Number Into Binary

This page will demonstrate how to convert a decimal number (i.e a whole number) into its binary equivalent. So for example, if the decimal number of 26 was entered into the program, it would display the converted binary value of 11010.

REQUIRED KNOWLEDGE FOR THIS SNIPPET

```How To Count In Binary The "Long" Datatype - What Is It? While Loops Online Binary to Decimal Converter - Verify For Correct Results How To Reverse A String ```

If you are looking for sample code which converts binary to decimal, check back here soon!

``` Convert A Decimal Number Into Binary C++ #include <iostream> #include <string> #include <algorithm> using namespace std; // function prototype string DecToBin(long long decNum); int main() { // declare variables long long decNum = 0; string binaryNum=""; // use a string instead of an int to avoid // overflow, because binary numbers can grow large quick cout<<"Please enter an integer value: "; cin >> decNum; if(decNum < 0) { binaryNum = "-"; } // call function to convert decimal to binary binaryNum += DecToBin(decNum); // display data to user cout<<"nThe integer value of "<<decNum<<" = "<<binaryNum<<" in binary"<<endl; return 0; } string DecToBin(long long decNum) { string binary = ""; // use this string to save the binary number if(decNum < 0) // if input is a neg number, make it positive { decNum *= -1; } // converts decimal to binary using division and modulus while(decNum > 0) { binary += (decNum % 2)+'0'; // convert int to char decNum /= 2; } // reverse the string reverse(binary.begin(), binary.end()); return binary; }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 #include <iostream>#include <string>#include <algorithm> using namespace std; // function prototypestring DecToBin(long long decNum); int main(){ // declare variables long long decNum = 0; string binaryNum=""; // use a string instead of an int to avoid   // overflow, because binary numbers can grow large quick  cout<<"Please enter an integer value: "; cin >> decNum;  if(decNum < 0) { binaryNum = "-"; }  // call function to convert decimal to binary binaryNum += DecToBin(decNum);  // display data to user cout<<"nThe integer value of "<<decNum<<" = "<<binaryNum<<" in binary"<<endl;  return 0;} string DecToBin(long long decNum){ string binary = ""; // use this string to save the binary number  if(decNum < 0) // if input is a neg number, make it positive {               decNum *= -1; }                     // converts decimal to binary using division and modulus while(decNum > 0) { binary += (decNum % 2)+'0'; // convert int to char decNum /= 2; }  // reverse the string reverse(binary.begin(), binary.end());  return binary;}// http://programmingnotes.org/ ```

The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output
Note: The code was compiled 3 separate times to display different output

`====== RUN 1 ======`

``` Please enter an integer value: 1987 The integer value of 1987 = 11111000011 in binary ====== RUN 2 ====== Please enter an integer value: -26 The integer value of -26 = -11010 in binary ====== RUN 3 ====== Please enter an integer value: 12345678910 ```

```The integer value of 12345678910 = 1011011111110111000001110000111110 in binary ```