Converting decimal to binary using recursion

Hey all, I'm trying to write a program that converts a decimal number to a binary one. I have most of the program written, but I am having a little bit of trouble. Whenever I enter a decimal number, the program will convert it correctly to binary, however, the last number is not included in the conversion. EX: Converting 37 into binary (0100101) yields 010010 when entered into the program. I cannot tell exactly what is going wrong, perhaps someone else can take a look? BTW the program must utilize recursion to achieve this goal.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream> 
using namespace std;
 
void decToBinary(int num1);
 
int main() 
{
    int num1;
 
    cout << "Enter a non-negative integer value: ";
    cin >> num1;
   
	if (num1 < 0)
	{
	cout << endl << "Invalid Entry." << endl << endl;
	}
	else 
	{
        cout << endl << "Decimal " << num1 << " = ";
        decToBinary(num1);
        cout << endl << endl;
        }

	return 0;
}
 
void decToBinary(int num1) 
{
    int remainder;
 
    num1 = num1 / 2;
    remainder = num1 % 2;
	
	if (num1 > 0)
	{
		decToBinary(num1);
		
	}
	else if (num1 = 0)
	{
		cout << "0";
		return;
	}
 
	cout << remainder;  
}



Last edited on
1) On line 40 you assign num1 with zero! What you wanted to do was

 
else if (num1 == 0)


2) You are also dividing num1 by 2 and reassign before you even calc the remainder, that's incorrect.

3) You should never directly modify the function parameter (in this case num1), make a copy and do something with it. Also use a const to make sure the input doesn't get modified.

4) When writing a recursion function, the 1st thing you MUST do is check the exit condition. Usually you don't need to have an if / else clause either!

1
2
3
4
5
6
7
8
9
10
void decToBinary( const int num1 )
{
   if( num1 == 0 )
   {
      return;
   }

   decToBinary( num1/2 );
   cout << num1 % 2;
}


--
Rajinder Yadav <labs@devmentor.org>

DevMentor Labs
http://labs.devmentor.org
Creating Amazing Possibilities
Last edited on
interchange lines 31 and 32. first find the remainder and then make num1 = num1/2
3) You should never directly modify the function parameter (in this case num1), make a copy and do something with it. Also use a const to make sure the input doesn't get modified.

I've not heard this before and I don't particularly agree with it.

For one thing, the parameter num1 is passed by value, it is already a copy of the original variable, so it is perfectly acceptable to change it as much as you like.

Another, even if the parameter was passed by reference, the reason is often because the whole intention is to modify the parameter.

Yes, there are some occasions when the parameter should not be changed, and it is correct to specify const in those cases, but there's no reason to do so this example.
Ah calculating the remainder before dividing fixed it, thanks for the suggestions everyone.
Topic archived. No new replies allowed.