# Thread: Converting Decimal to Binary Numbers using Recursion

1. ## Converting Decimal to Binary Numbers using Recursion

I am trying to make a program that will convert a decimal number to its binary equivilant. It will output the first number but I am having trouble figuring out how to use the previously found quotient to find a new quotient. I tried using loops to do this and they are always infinite.

Here is the code I have written so far

Code:
```#include <iostream>
using namespace std;

void convert (int);

int main ()

{
int num;

cout <<"Please enter a number to convert" <<endl;
cin>>num;

convert (num);
return 0;

}

void convert (int num)

{

if (num>0)
{
double quotient,remainder;

quotient=num/2;
remainder=num%2;

if (quotient==0)
cout <<" ";
else
{

if (remainder==0)
cout<<"0";
else if (remainder==1)
cout<<"1";

}
}

else
cout<< " ";

}```

2. If I understand what you want to do correctly, loops will be the way to go. What loops have you tried? It was most likely a problem with the way you implemented the loop that was causing a problem, as opposed to the idea of using loops messing things up.

3. Jaisch. you're on the right track, but:
1: you forgot the recursion; i.e. you didn't deal with the whole situation when quotient != 0
2: You still have more to do when quotient == 0
3: Your algorithm is going to find 2^0, then 2^1, then 2^2, etc., which is fine but you're going to end up printing the binary number in reverse unless you do something to handle that.
4: Why output anything when num == 0? Just return.