C++ Program to convert a Number from Decimal to Binary (4 Ways)

In this program, we will learn how to convert a number from decimal to binary using C++ programming language.

We will be discussing four ways to code it.

  • Using an array
  • Without using an array
  • Using Bitwise operator
  • Using Recursion

C++ Programming Code to convert a Number from Decimal to Binary

Using an Array

In this program, I have stored the remainder when num is divided by 2 (num%2) in an array. And then using the for loop printing the elements of array in reverse order.

Code:-

#include <iostream>  

using namespace std;  

int main(){  
    int bin[128], num, i=0;    
    cout << "Enter a number to convert: ";    
    cin >> num;    
    while(num > 0){    
        bin[i]=num%2;    
        num= num/2; 
        i++; 
    }   

    cout << "Binary of the number: ";

    for(int j = i-1; j >= 0; j--){    
        cout << bin[j];    
    }  

    cout << endl;

    return 0;  
}  

Output:-

Enter a number to convert: 55
Binary of the number: 110111

Without using an Array

In this program, I have not used an array to store individual bits of the number instead I build it using while loop and adding the product of remainder and power of 10 to bin and storing it back to the bin.

Code:-

#include <iostream>

using namespace std;

int main(){
    int num, bin = 0;

    cout << "Enter a number to convert: ";    
    cin >> num; 

    int remainder, i = 1;

    while (num!=0){
        remainder = num%2;
        num /= 2;
        bin += remainder*i;
        i *= 10;
    }
    cout << "Binary of the number: " << bin << endl;
    
    return 0;
}

Output:-

Enter a number to convert: 64
Binary of the number: 1000000

Using BitWise Operator

In this program, I have used the AND (&) and Right shift (>>) bitwise operator.

Code:-

#include <iostream>

using namespace std;


int main(){
    int num;
    unsigned int range = 32768;

    cout << "Enter a number to convert: ";    
    cin >> num; 

    cout << "Binary of the number: ";
    
    while(range > 0){
        if((num & range) == 0 )
            cout << "0";
        else
            cout << "1";
        range = range >> 1 ; 
    }

    cout << endl;
    
    return 0;
}

Output:-

Enter a number to convert: 76
Binary of the number: 0000000001001100

Using Recursion

In this program, I have used the getBinaryNumber() recursive function.

Code:-

#include <iostream>

using namespace std;

int getBinaryNumber(int num) { 
    if (num == 0)  
        return 0;  
    else
        return (num % 2 + 10 * getBinaryNumber(num / 2)); 
} 

int main(){
    int num, bin;

    cout << "Enter a number to convert: ";    
    cin >> num; 

    int remainder, i = 1;

    bin = getBinaryNumber(num);

    cout << "Binary of the number: " << bin << endl;
    
    return 0;
}

Output:-

Enter a number to convert: 85
Binary of the number: 1010101

You can learn about many other C++ Programs Here.

Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...