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++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th 5 min read Inheritance in C++ The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio 10 min read 30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is 15 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Templates in C++ C++ template is a powerful tool that allows you to write a generic code that can work with any data type. The idea is to simply pass the data type as a parameter so that we don't need to write the same code for different data types.For example, same sorting algorithm can work for different type, so 9 min read Operator Overloading in C++ in C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot 8 min read C++ Classes and Objects In C++, classes and objects are the basic building block that leads to Object-Oriented programming in C++. We will learn about C++ classes, objects, look at how they work and how to implement them in our C++ program.C++ ClassesA class is a user-defined data type, which holds its own data members and 9 min read C++ Interview Questions and Answers (2025) C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i 15+ min read C++ Polymorphism The word polymorphism means having many forms. A real-life example of polymorphism is a person who at the same time can have different characteristics. A man at the same time is a father, a husband, and an employee. So, the same person exhibits different behaviour in different situations. This is ca 5 min read Like