Write a recursive function in C programming to find reverse of any number. How to find reverse of any number in C programming using recursion. Finding reverse of any number using recursion in C programming.

Input number: 12345

Output reverse: 54321

reverse(0) = 0

reverse(n) = (n%10 * pow(10, digits)) + reverse(n/10)

Happy coding ;)

**Example:**Input number: 12345

Output reverse: 54321

### Required knowledge

Basic C programming, Function, Recursion### Logic to find reverse of number using recursion

We have already seen how to find reverse of any number using simple loops here we will see how efficiently we can find reverse of any given number using recursion. If you have gone through the reverse procedure through loop you probably know that reversing procedure includes four basic steps i.e.- Multiply the rev variable by 10.
- Find the last digit of the given number.
- Add the last digit just found to rev.
- Divide the original number by 10 to eliminate the last digit, which is not needed anymore.

reverse(0) = 0

**{Base condition}**reverse(n) = (n%10 * pow(10, digits)) + reverse(n/10)

**{where**digit**is the total number of digits in number}**### Program to find reverse using recursion

/** * C program to find reverse of any number using recursion */ #include <stdio.h> #include <math.h> /* Fuction declaration */ int reverse(int num); int main() { int num, rev; /* Reads number from user */ printf("Enter any number: "); scanf("%d", &num); /*Calls the function to reverse number */ rev = reverse(num); printf("Reverse of %d = %d", num, rev); return 0; } /** * Recursive function to find reverse of any number */ int reverse(int num) { int digit; //Base condition if(num==0) return 0; //Finds total number of digits digit = (int)log10(num); return ((num%10 * pow(10, digit)) + reverse(num/10)); }

Output

Enter any number: 12345

Reverse of 12345 = 54321

Reverse of 12345 = 54321

Happy coding ;)

### You may also like

- Function and recursion programming exercises index.
- C program to check palindrome numbers using recursion.
- C program to find power of any number using recursion.
- C program to print all natural numbers from 1 to n using recursion.
- C program to print sum of natural numbers from 1 to n using recursion.
- C program to find factorial of any number using recursion.
- C program to find nth fibonacci term using recursion.
- C program to find GCD(HCF) of two numbers using recursion.
- C program to check prime, armstrong, perfect numbers using functions.
- C program to print all prime numbers between given interval using functions.
- C program to find all armstrong numbers between given interval using functions.
- C program to find all strong numbers between given interval using functions.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).