C++ Online Compiler
Example: C++ program to convert decimal to binary using recursion
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ program to convert decimal to binary using recursion #include <iostream> using namespace std; // Recursive function to convert and print binary void convertToBinary(int num) { if (num == 0) return; // Recursive call convertToBinary(num / 2); // Print remainder cout << (num % 2); } // The main function of the program int main() { // Step-1 Declare variable int num; // Step-2 Input decimal number cout << "Enter a decimal number: "; cin >> num; // Step-3 Print binary using recursion cout << "Binary representation: "; if (num == 0) cout << "0"; else convertToBinary(num); cout << endl; return 0; }
Output
Clear
ADVERTISEMENTS