GFG App
Open App
Browser
Continue

# C++ Program For Decimal To Binary Conversion

Given a decimal number as input, we need to write a program to convert the given decimal number into an equivalent binary number.

Examples:Â

```
Input: 7
Output: 111

Input: 10
Output: 1010

Input: 33
Output: 100001```

For Example:Â
If the decimal number is 10.Â
Step 1: Remainder when 10 is divided by 2 is zero. Therefore, arr[0] = 0.Â
Step 2: Divide 10 by 2. New number is 10/2 = 5.Â
Step 3: Remainder when 5 is divided by 2 is 1. Therefore, arr[1] = 1.Â
Step 4: Divide 5 by 2. New number is 5/2 = 2.Â
Step 5: Remainder when 2 is divided by 2 is zero. Therefore, arr[2] = 0.Â
Step 6: Divide 2 by 2. New number is 2/2 = 1.Â
Step 7: Remainder when 1 is divided by 2 is 1. Therefore, arr[3] = 1.Â
Step 8: Divide 1 by 2. New number is 1/2 = 0.Â
Step 9: Since number becomes = 0. Print the array in reverse order. Therefore the equivalent binary number is 1010.

The below diagram shows an example of converting the decimal number 17 to an equivalent binary number.Â

Below is the implementation of the above idea.Â Â

## C++

 `// C++ program to convert a decimal ` `// number to binary number ` `#include ` `using` `namespace` `std; ` ` `  `// Function to convert decimal  ` `// to binary ` `void` `decToBinary(``int` `n) ` `{ ` `    ``// Array to store binary number ` `    ``int` `binaryNum[32]; ` ` `  `    ``// Counter for binary array ` `    ``int` `i = 0; ` `    ``while` `(n > 0)  ` `    ``{ ` `        ``// Storing remainder in binary  ` `        ``// array ` `        ``binaryNum[i] = n % 2; ` `        ``n = n / 2; ` `        ``i++; ` `    ``} ` ` `  `    ``// Printing binary array in reverse  ` `    ``// order ` `    ``for` `(``int` `j = i - 1; j >= 0; j--) ` `        ``cout << binaryNum[j]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 17; ` `    ``decToBinary(n); ` `    ``return` `0; ` `} `

Output:

`10001`

Time complexity: O(logn)Â

Auxiliary Space: O(1)Â

We can use bitwise operators to do the above job. Note that bitwise operators work faster than arithmetic operators used above.

## C++

 `// C++ program to Decimal to binary  ` `// conversion using bitwise operator ` `// Size of an integer is assumed to  ` `// be 32 bits ` `#include ` `using` `namespace` `std; ` ` `  `// Function that convert Decimal  ` `// to binary ` `int` `decToBinary(``int` `n) ` `{ ` `    ``// Size of an integer is assumed  ` `    ``// to be 32 bits ` `    ``for` `(``int` `i = 31; i >= 0; i--)  ` `    ``{ ` `        ``int` `k = n >> i; ` `        ``if` `(k & 1) ` `            ``cout << ``"1"``; ` `        ``else` `            ``cout << ``"0"``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 32; ` `    ``decToBinary(n); ` `} `

Output:

`00000000000000000000000000100000`

Time complexity: O(1)Â

loop iterates constant(32) number of times everytime even for small numberÂ

Auxiliary Space: O(1)

### Efficient Approach:Â

It’s another efficient approach to converting Decimal to binary using the right shift(>>) and And(&) operator. Here we’ll use only Binary Operators which usually are very fast in computation.

## C++

 `// C++ program to implement  ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `string DecimalToBinary(``int` `num) ` `{ ` `    ``string str; ` `      ``while``(num) ` `      ``{ ` `      ``// 1 ` `      ``if``(num & 1)  ` `        ``str+=``'1'``; ` `       `  `      ``// 0 ` `      ``else`  `        ``str+=``'0'``; ` ` `  `      ``// Right Shift by 1   ` `      ``num>>=1;  ` `    ``}     ` `      ``return` `str; ` `} ` ` `  `void` `reverse(string str) ` `{ ` `  ``for``(``int` `i=str.size()-1 ; i>=0 ; i--) ` `   ``cout<< str[i];  ` `} ` ` `  `// Driver code ` `int` `main()  ` `{ ` `    ``int` `num = 59; ` `    ``cout<< ``"Binary of num 59 is: "``; ` `    ``reverse( DecimalToBinary(num)); ` `    ``return` `0; ` `} `

Output:

`Binary of num 59 is: 111011`

Time Complexity: Â O(log n)

Auxiliary Space: O(1)Â

Decimal to binary conversion can also be done without using arrays.Â

## C++

 `// C++ implementation of the approach ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `#define ull unsigned long long int ` ` `  `// Function to return the binary ` `// equivalent of decimal value N ` `int` `decimalToBinary(``int` `N) ` `{ ` ` `  `    ``// To store the binary number ` `    ``ull B_Number = 0; ` `    ``int` `cnt = 0; ` `    ``while` `(N != 0)  ` `    ``{ ` `        ``int` `rem = N % 2; ` `        ``ull c = ``pow``(10, cnt); ` `        ``B_Number += rem * c; ` `        ``N /= 2; ` ` `  `        ``// Count used to store exponent value ` `        ``cnt++; ` `    ``} ` ` `  `    ``return` `B_Number; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 17; ` `    ``cout << decimalToBinary(N); ` `    ``return` `0; ` `} `

Output:

`10001`

Time complexity: O(logn)Â

Auxiliary Space: O(1)Â

Note that this method is similar to the one where we convert Binary to Decimal as discussed in this post.
There is yet another method that converts any Decimal Number to its Binary form. The idea is to use bitset.

Below is the implementation of the above approach.Â

## C++

 `// C++ program to convert a decimal number ` `// to its binary form. ` ` `  `// including header file ` `#include ` `using` `namespace` `std; ` ` `  `// Function to convert a decimal number ` `// to its binary form ` `string decimalToBinary(``int` `n) ` `{ ` `    ``// Finding the binary form of the number  ` `    ``// and converting it to string.  ` `    ``string s = bitset<64> (n).to_string(); ` `     `  `    ``// Finding the first occurrence of "1" ` `    ``// to strip off the leading zeroes. ` `    ``const` `auto` `loc1 = s.find(``'1'``); ` `     `  `    ``if``(loc1 != string::npos) ` `        ``return` `s.substr(loc1); ` `     `  `    ``return` `"0"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 17; ` `     `  `    ``// Function call ` `    ``cout << decimalToBinary(n); ` ` `  `    ``return` `0; ` `} `

Output:

`10001`

Time complexity: O(logn)Â

Auxiliary Space: O(1)Â
Another Approach:

## C++

 `// C++ program to convert Decimal  ` `// to Binary Number ` `#include ` `using` `namespace` `std; ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Input number ` `    ``int` `number = 15; ` `    ``int` `n = (``int``)(log2(number)); ` `   `  `    ``// Binary output ` `    ``// using the inbuilt function ` `    ``cout << ``"the binary number is : "` `<<  ` `             ``bitset<64>(number).to_string().substr(64 - n - 1); ` `} `

Output:

`the binary number is : 1111`

Time complexity: O(logn)Â

Auxiliary Space: O(1)Â

My Personal Notes arrow_drop_up