Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

C++ Program to Convert Decimal Number to Binary



In a computer system, the binary number is expressed in the binary numeral system while the decimal number is in the decimal numeral system. The binary number is in base 2 while the decimal number is in base 10. Examples of decimal numbers and their corresponding binary numbers are as follows:

Decimal Number Binary Number
15 01111
10 01010
18 10010
27 11011
We can convert a decimal number to binary using different methods. Some simple ways are:

Decimal to Binary Conversion Using Repeated Division by 2

In this approach, we repeatedly divide the decimal number by 2 and store the remainders. These remainders represent the binary digits. Once the number becomes 0, the collected digits are reversed to give the final binary number.

Example

Here's a C++ program where we use a loop to divide the decimal number by 2, storing the remainders. Once we've collected all the binary digits, we print them in reverse order to display the correct binary representation.

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int decimal = 27;   
    int original = decimal; 
    vector<int> binary;     // To store binary digits

    // Special case when the number is 0
    if (decimal == 0) {
        binary.push_back(0);
    } else {
        // Repeat division by 2 and store remainders
        while (decimal > 0) {
            binary.push_back(decimal % 2); // Store remainder (0 or 1)
            decimal /= 2;                  // Update the number
        }
    }
    cout << "Decimal Number: " << original << endl;
    // Output the binary number (reversed remainders)
    cout << "Binary Number: ";
    for (int i = binary.size() - 1; i >= 0; i--) {
        cout << binary[i];
    }
    cout << endl;
    return 0;
}

The output of the program is shown below.

Decimal Number: 27
Binary Number: 11011

Time Complexity: O(log N) because we divide the number by 2 each time.

Space Complexity: O(log N) because we store the each binary digit.

Decimal to Binary Conversion Using Recursion

In this approach, we recursively divide the number by 2, storing the remainders, and print them in reverse order once the recursion completes, resulting in the binary representation.

Example

In this example, the function decimalToBinary() divides the number by 2 repeatedly and prints the remainders. It continues until the base case (n = 0) is reached, and then prints the binary digits in reverse order.

#include <iostream>
using namespace std;

// Recursive function to convert decimal to binary
void decimalToBinary(int n) {
    if (n == 0) return; // Base case: no digits left
    
    decimalToBinary(n / 2); // Recur for n/2
    cout << n % 2;          // Print the remainder (binary digit)
}

int main() {
    int number = 27; // Example number to convert
    
    cout << "Decimal number: " << number << endl;
    
    cout << "Binary: ";
    if (number == 0) {
        cout << 0;
    } else {
        decimalToBinary(number);
    }
    return 0;
}

Below, you can see the output of the program.

Decimal number: 27
Binary: 11011

Time Complexity: O(log n) as the function recurses until the number becomes 0.

Space Complexity: O(log n) due to the recursive call stack.

Decimal to Binary Conversion Using Bitwise Operations

In this approach, we use bitwise operations to extract each binary digit. We repeatedly shift the number to the right and use the bitwise AND operation to get the least significant bit.

Example

In this example, we use bitwise operations to get the binary digits. The bitwise AND (n & 1) gives the last bit, and the right shift (n >> 1) removes it. This repeats until the number is 0, and then we print the binary digits in reverse.

#include <iostream>
using namespace std;

// Function to convert decimal to binary using bitwise operations
void decimalToBinary(int n) {
    if (n == 0) {
        cout << "Binary: 0" << endl;
        return;
    }
    
    int binary[32]; // Array to store binary digits
    int index = 0;
    
    // Extract each binary digit using bitwise operations
    while (n > 0) {
        binary[index] = n & 1; // Extract the least significant bit
        n = n >> 1;            // Right shift the number
        index++;
    }
    
    // Printing the binary number in reverse order
    cout << "Binary: ";
    for (int i = index - 1; i >= 0; i--) {
        cout << binary[i];
    }
    cout << endl;
}

int main() {
    int number = 27; // Example number to convert
    
    cout << "Decimal number: " << number << endl;
    
    // Convert to binary
    decimalToBinary(number);
    
    return 0;
}

Below is the program's output for the given input.

Decimal number: 27
Binary: 11011

Time Complexity: O(log n) due to the right shift operation and checking bits.

Space Complexity: O(1) since we only use a fixed-size array to store the binary digits.

Updated on: 2025-05-13T16:58:31+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements