# Write a recursive function to get the reverse of a given integer

*Write a recursive function to get the reverse of a given integer. When a function calls itself it is called recursive. Calling a function from the function definition itself is way of looping. It means you have to introduce a terminating condition inside the function.
*

*Sample code: write a recursive function to get the reverse of a given integer*

Suppose you are given a positive number 123409 then your program should give 904321. How we proceed for the solution?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int func (int n, int c) { if (n == 0) return c; c = c * 10 + n % 10; return func (n / 10, c); } int main () { int n = 123409; int r = 0; r = func (n, 0); printf ("%d\n", r); return 0; } |

*What we have done*

*Take the last digit, multiply 10 with previous step result.**Add the current digit to it.**send it to next function to do the same for the remaining digits.*

Ref:

https://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/recursion2.html

## Leave a Reply