Howdy readers, today we will learn **how to convert decimal to binary using C++** Programming language.

To convert decimal to binary, follow these steps:

- Take a number and divide it by
**2**. - Take the
**quotient**and divide it by**2**until you reach zero. - Each time you divide the number, note down the
**remainder**and then reverse the remainder list. You get the number in the binary form.

Suppose, we have a decimal number 27, then:

- 27 / 2 = 13 giving remainder
**1** - 13 / 2 = 6 giving remainder
**1** - 6 / 2 = 3 giving remainder
**0** - 3 / 2 = 1 giving remainder
**1** - 1 / 2 = 0 giving remainder
**1**

We get **(27) _{10} = (11011)_{2}**.

We will be using the above logic in this tutorial to convert decimal to binary.

So, without any delay, letâ€™s begin this tutorial.

**C++ Program to Convert Decimal to Binary**

**C++ Program**

// C++ Program to Convert Decimal to Binary #include <iostream> using namespace std; int main(){ int num, i = 1, rem, binary = 0; // Asking for input cout << "Enter a decimal number: "; cin >> num; // Decimal to binary using while loop while (num != 0){ rem = num % 2; num = num / 2; binary = binary + rem * i; i = i * 10; } // Printing output cout << "Equivalent binary number is: " << binary << endl; return 0; }

**Output**

Enter a decimal number: 15 Equivalent binary number is: 1111

**Explanation**

int num, i = 1, rem, binary = 0;

We have declared four int data type variables named** num**,** i**, **rem** and **binary**.

// Asking for input cout << "Enter a decimal number: "; cin >> num;

Then, this program asks the user to enter a decimal number. This number gets stored in the **num** named variable.

// Decimal to binary using while loop while (num != 0){ rem = num % 2; num = num / 2; binary = binary + rem * i; i = i * 10; }

Suppose the entered number is** 15**, then:

num != 0 | num%2 = rem | num/2 = num | binary + rem*i = binary | i * 10 = i |
---|---|---|---|---|

15 != 0 | 15 % 2 = 1 | 15/2 = 7 | 0 + 1*1 = 0 + 1 = 1 | 1*10 = 10 |

7 != 0 | 7 % 2 = 1 | 7/2 = 3 | 1 + 1*10 = 1 + 10 = 11 | 10*10 = 100 |

3 != 0 | 3 % 2 = 1 | 3/2 = 1 | 11 + 1*100 = 11 + 100 = 111 | 100*10=1000 |

1 != 0 | 1 % 2 = 1 | 1 / 2 = 0 | 111 + 1*1000 = 111 + 1000 = 1111 | 1000*10 |

0 = 0 | – | – | – | – |

Hence, we get **(15) _{10} = (1111)_{2}**.

// Printing output cout << "Equivalent binary number is: " << binary << endl;

Finally, the equivalent binary number is displayed on the screen using the **cout **statement.

**C++ Program to Convert Decimal to Binary Using Functions**

**C++ Program**

// C++ Program to Convert Decimal to Binary Using Functions #include <iostream> using namespace std; int binary_num(int num){ int i = 1, rem, binary = 0; while(num != 0){ rem = num % 2; num = num / 2; binary = binary + (rem*i); i = i * 10; } return binary; } int main(){ int n, bin; // Asking for input cout << "Please enter a decimal number: "; cin >> n; // Calling out user-defined function bin = binary_num(n); // Displaying result cout << "Equivalent binary number is: " << bin << endl; return 0; }

**Output**

Please enter a decimal number: 17 Equivalent binary number is: 10001

**Explanation**

int binary_num(int num){ int i = 1, rem, binary = 0; while(num != 0){ rem = num % 2; num = num / 2; binary = binary + (rem*i); i = i * 10; } return binary; }

Here, we have defined a custom function named **binary_num** which converts decimal numbers into binary numbers using a **while loop**.

**Conclusion**

I hope after going through this tutorial, you understand **how to write a program to convert decimal to binary using C** Programming language.

If you have any doubt regarding the tutorial, then let us know in the comment section. We will be pleased to solve your doubts.