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.
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.
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
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.