Create a recursive function dec to bin to find the binary representation of a non-negative integer.
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Recursive function to convert decimal
// to its binary equivalent
void decimalToBinary(int n)
{
// Base case
if (n == 0) {
cout << "0";
return;
}
// Recursive call
decimalToBinary(n / 2);
cout<<n%2;
}
// Driver code
int main()
{
int n;
cout<<"Enter the Decimal number: "<<endl;
cin>>n;
cout<<"The binary number is: ";
decimalToBinary(n);
return 0;
}
Comments
Leave a comment