diff --git a/Conversions/C:C++/decimal_to_binary.cpp b/Conversions/C:C++/decimal_to_binary.cpp new file mode 100644 index 00000000..41e931f8 --- /dev/null +++ b/Conversions/C:C++/decimal_to_binary.cpp @@ -0,0 +1,31 @@ +#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 program to test above function +int main() +{ + int n = 17; + decToBinary(n); + return 0; +}