 # C Programming Code Examples

## C > Functions Code Examples

### C program to find reverse of a number using recursion

``` C program to find reverse of a number using recursion Write a recursive function in C programming to find reverse of a number. How to find reverse of a number in C programming using recursion. Declare recursive function to find reverse of a number First let us give a meaningful name to our function, say reverse(). The function computes reverse of number, hence it must accept an integer parameter. So let us update our function declaration reverse(int j). Finally, the function computes and returns an integer which is reverse of given number. After looking on above points function declaration to find reverse of a number is - int reverse(int j); Reversing an integer involves four basis steps. Multiply reverse variable by 10. Find the last digit of the given number. Add last digit just found to reverse. Divide the original number by 10 to remove last digit, which is not needed anymore. Repeat above four steps till the number becomes 0 and we are left with the reversed number in reverse variable. Here also we will use the above four steps to find reverse using recursive approach with the given base condition: reverse(0) = 0 {Base condition} reverse(n) = (n%10 * pow(10, digits)) + reverse(n/10) {where digit is number of digits in number} #include <stdio.h> #include <math.h> /* Fuction declaration */ int reverse(int j); int main() { int j, rev; /* Input number from user */ printf("Enter any number: "); scanf("%d", &j); /* Call the function to reverse number */ rev = reverse(j); printf("Reverse of %d = %d", j, rev); return 0; } /* Recursive function to find reverse of any number */ int reverse(int j) { // Find total digits in j int digit = (int) log10(j); // Base condition if(j == 0) return 0; return ((j%10 * pow(10, digit)) + reverse(j/10)); } ``` 