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?
func (int n, int c)
if (n == 0)
c = c * 10 + n % 10;
return func (n / 10, c);
int n = 123409;
int r = 0;
r = func (n, 0);
printf ("%d\n", r);
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.