C++ Program to Convert Decimal to Binary

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

C++ Program to Convert Decimal to Binary

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;
    
    // 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
C++ Program to Convert Decimal to Binary

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 != 0num%2 = remnum/2 = numbinary + rem*i = binaryi * 10 = i
15 != 015 % 2 = 115/2 = 70 + 1*1 = 0 + 1 = 11*10 = 10
7 != 07 % 2 = 17/2 = 31 + 1*10 = 1 + 10 = 1110*10 = 100
3 != 03 % 2 = 13/2 = 111 + 1*100 = 11 + 100 = 111100*10=1000
1 != 01 % 2 = 11 / 2 = 0111 + 1*1000 = 111 + 1000 = 11111000*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
C++ Program to Convert Decimal to Binary Using Functions

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.

Leave a Comment