Answer to Question #265552 in C++ for Usama

Question #265552

Create a recursive function dec to bin to find the binary representation of a non-negative integer.


1
Expert's answer
2021-11-16T07:44:41-0500
// 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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS