How to Convert Decimal to Binary in C/C++ Program?

How to Convert Decimal to Binary in C/C++ Program?

If you are searching for an inbuild function to convert the given integer value into binary format, let me be clear it is not there. In fact, there is no boolean data type in C. So we have to convert decimal to Binary in C manually.

In many programming cases, you need to perform bitwise operations on the Binary format of an integer value. For example, you want to count a number of bits in the binary representation of the given number.

In C programming, to perform bit manipulation operations, many times, you need to convert the decimal number to binary number.

Here is a simple online tool to convert decimal to binary.

Now we want to write a C program for this conversion.

C/C++ Program to Convert Decimal to Binary

You can solve this problem with or without recursion.

With Recursion in C

Following is the code snippet that convert Decimal to Binary in C.

#include<stdio.h>
//Function takes input as integer 
//and return integer value in binary format
unsigned intToBin(unsigned k)
{
    if (k == 0) return 0;
    if (k == 1) return 1;
    return (k % 2) + 10 * intToBin(k / 2);
}
 
void main()
{
    int k = 34;
    char str[100] = {0};
    printf("%d (Decimal) = %u (Binary)", k, intToBin(k));
}

In the above code, I am using the recursion programming technique. You can also use a loop and break statement.

Output:

34 (Decimal) = 100010 (Binary)

In this code for 34, you are getting the output as 100010.

Note: Both the input and output of the variable of the function intToBin() are an integer data types. You can save the binary representation of the number either in an integer or a string data type variable.

Without Recursion in C++

Algorithm and Approach:

  1. First we need to divide the number by two and store the remainder in an array of size 32.
  2. Number = Number/2
  3. Repeat steps 1 & 2 until the number is greater than zero.
  4. Print the array in reverse order, that will be your binary form.

C++ Code:

// C++ program to convert a decimal
// number to binary number
#include <iostream>
using namespace std;

int main()
{
    int n = 13;

    // array to store binary number
    int binary[32];

     int i = 0;
     while (n > 0) {
 
        // storing remainder in binary array
        binary[i] = n % 2;
        n = n / 2;
        i++;
    }
    
    // print binary array in reverse order
    for (int k = i - 1; k >= 0; k--)
        cout << binary[k];

  return 0;
}

Output:

1101

This algorithm and code are contributed by Om Tayade.

This is one of the tricky questions asked in many job interview rounds. You can find out more about such kinds of questions in the list of complete coding interview questions.

To practice more of these bitwise coding questions, check data structure coding interview questions.

There are many methods you can use to convert decimal to an integer. If you have any doubt or if you can suggest any other efficient method of doing this conversion, discuss it in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *