954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Recursive Function to Reverse an Integer

I have an assignment to write a recursive function that takes one integer variable and output the integer in reverse order to the screen. I've tried converting it to a char array(c++ "string" class not allowed) but could not get a counter to work. I can reverse an int array with and "upper" and "lower" variable no problem, but I'm stuck without them.

???? reverse(int num) //This is what the assignment calls for.

I'm not looking for anyone to write it for me but I would appreciate some tips.
Thanks in advance.

badmofo
Newbie Poster
3 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Why would you need to use a char array? using int array should be fine.

reverse (num)
array[count] = num
count ++
get a new num
reverse (num)
print out array[count]
count--

It might be logically flawed somewhere..just make sure you have a base case (where you would stop calling the reverse function).

freudian_slip
Light Poster
36 posts since Oct 2008
Reputation Points: 11
Solved Threads: 4
 

If you're just displaying the reversed thing to the screen, there is no need to store any numbers in any arrays.

Each call to the function will print out a single digit (the digit of number in the units place). If the number is more than 9, the function will call itself with number/10 as the argument. This will recursively print out the number in reverse. When all digits have been printed, the function will stop, and return, due to the above stated call condition.

dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

raw scetch of how I should start to do it:
revprint(int n)
{
if n==0 return
cout<

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

I'd do it that way too, just I'd add a little if-else-abs() trick to handle n<0 (otherwise it would print -567 reversed as -7-6-5).

something like

if(n<0) {
     cout << "-" << abs(n%10);
     revprint(n/-10);
}
else {
     cout << n%10;
     revprint(n/10);
}
mrboolf
Junior Poster
183 posts since Jun 2008
Reputation Points: 134
Solved Threads: 18
 

Thanks everyone! I got it working using "n % 10" I was making it too complicated as usual

badmofo
Newbie Poster
3 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

So you can mark this as solved.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

if we give a input which has a digit 0 in between it then loop will end there and output will be incorrect
for eg 1034
the loop will end at the second digit 0 and otput will be incorrect

what to do in this case??

anuj khasgi
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You