# C++ Program to Convert Decimal to Binary

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

To convert decimal to binary, follow these steps:

1. Take a number and divide it by 2.
2. Take the quotient and divide it by 2 until you reach zero.
3. 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;

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:

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;

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.