C++ Program For Decimal To Binary Conversion Last Updated : 21 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recommended PracticeDecimal to binaryTry It!Decimal to Binary Conversion using LoopCreate an array to store the binary representation of size 32, Since the binary representation of an integer can have at most 32 bits.Run a loop till n is greater than 0.Extract the remainder by taking the mod of number using the modulus operator and store the remainder in the array as a binary digit.Update the number by dividing it by 2 in each iteration.Print the array in reverse order.C++ Program to Convert Decimal To Binary using Loop C++ // C++ program to convert a decimal // number to binary number #include <iostream> 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 = 10; decToBinary(n); return 0; } Output1010Explanation 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. The 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. The 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. The 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. The new number is 1/2 = 0. Step 9: Since the number becomes = 0.Print the array in reverse order. Therefore the equivalent binary number is 1010. Complexity AnalysisTime complexity: O(log (n))Auxiliary Space: O(1)Decimal to Binary Conversion using std::bitset C++ Standard Template Library provides std::bitset class that is used to perform bitwise operations on binary numbers. It can be used to convert Decimal Numbers to Binary and vice versa. C++ Program to Convert Decimal To Binary using std::bitset In the below C++ program, a bitset of size 4 is created and initialized with 10. Now the bitset binaryRepresentation will store the binary representation of 10. C++ // C++ program to convert decimal numbers to binary using // bitset class #include <bitset> #include <iostream> #include <math.h> using namespace std; int main() { int decimalNumber = 10; // Assuming 32-bit representation const int sz = ceil(log2(10)); bitset<sz> binaryRepresentation(decimalNumber); cout << "Binary representation: " << binaryRepresentation << endl; return 0; } OutputBinary representation: 1010Complexity AnalysisTime complexity: O(log(n)), where n is the decimal number.Auxiliary Space: O(1) Refer to the complete article Program for Decimal to Binary Conversion for more methods to convert decimal numbers to binary numbers. Comment More infoAdvertise with us Next Article C++ Program For Decimal To Binary Conversion kartik Follow Improve Article Tags : C++ Programs C++ C++ Conversion Programs Practice Tags : CPP Similar Reads C++ Program For Binary To Decimal Conversion The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v 4 min read C++ Program For Decimal To Octal Conversion The octal numbers are a base 8 number system that uses digits from 0-7 and the decimal numbers are a base 10 numbers system that uses 10 digits from 0-9 to represent any numeric value. In this article, we will learn how to write a C++ program to convert a given decimal number into an equivalent octa 2 min read C++ Program For Octal To Decimal Conversion Given an octal number as input, we need to write a program to convert the given octal number into an equivalent decimal number. Examples: Input : 67Output: 55 Input : 512Output: 330 Input : 123Output: 83 1. Simple ApproachThe idea is to extract the digits of a given octal number starting from the ri 2 min read C++ Program For Binary To Octal Conversion The problem is to convert the given binary number (represented as a string) to its equivalent octal number. The input could be very large and may not fit even into an unsigned long long int. Examples: Input: 110001110Output: 616 Input: 1111001010010100001.010110110011011Output: 1712241.26633 Simple 5 min read C++ Program For Hexadecimal To Decimal Conversion The hexadecimal numbers are base 16 numbers that use 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all digits. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). Decimal numbers are base 10 numbers with 10 symbols to represent all digits. In this article, we will l 3 min read C++ Program For Decimal To Hexadecimal Conversion In this article, we will learn to write a C++ program to convert a decimal number into an equivalent hexadecimal number. i.e. convert the number with base value 10 to base value 16. In the decimal system, we use ten digits (0 to 9) to represent a number, while in the hexadecimal system, we use sixte 3 min read Recursive Program for Binary to Decimal Given a binary number as string, find its decimal equivalent. Examples: Input : binary = "101"Output : 5 Input : binary = "1111"Output : 15 We have discussed iterative solution to convert Binary to Decimal.The idea is simple, we add current term and recur for remaining terms. C++ // Recursive CPP pr 3 min read C++ Program For char to int Conversion In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.Example:Input: '9'Output: 9Ex 2 min read C++ Program For Double to String Conversion Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76 2 min read Bit Magic C/C++ Programs Bit manipulation, also known as bit magic is the process of using bit-level operations to manipulate individual bits of a number. It uses bitwise operators such as AND, OR, XOR, right shift, etc. to manipulate, set, and shift the individual bits. This is used to improve the efficiency of our program 3 min read Like