Program to convert a decimal number to binary using recursion in C++
Solution:
#include <iostream> using namespace std; long conversion(int n) { static long bin,r,factor = 1; if(n != 0) { r = n % 2; bin = bin + r * factor; factor = factor * 10; conversion(n / 2); } return bin; } int main() { int n,ans; cout<<"Enter a decimal number "; cin>>n; ans = conversion(n); cout << "Binary Number of "<< n<<" is "<<ans; return 0; }
Output :
Enter a decimal number 23 Binary Number of 23 is 10111