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. Logic to find reverse of a number using recursion in C programming

Example

Input

Input number: 12345

Output

Reverse: 54321

Required knowledge

Basic C programming, If else, Functions, Recursion

Must know - Program to find reverse of a number using loop

Declare recursive function to find reverse of a number

  1. First let us give a meaningful name to our function, say reverse().
  2. The function computes reverse of number, hence it must accept an integer parameter. So let us update our function declaration reverse(int num).
  3. 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 num);

Logic to find reverse of number using recursion

Reversing an integer involves four basis steps.

  1. Multiply reverse variable by 10.
  2. Find the last digit of the given number.
  3. Add last digit just found to reverse.
  4. 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}

Program to find reverse of a number 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;

    /* Input number from user */    
    printf("Enter any number: ");
    scanf("%d", &num);
    
    /* Call 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)
{
    // Find total digits in num
    int digit = (int) log10(num);
    
    // Base condition
    if(num == 0)
        return 0;
    
    return ((num%10 * pow(10, digit)) + reverse(num/10));
}

The above program works fine. However, as mentioned by one of our reader it doesn't works for negative values. This is because we are using log10() function and logarithm is only defined for positive values. Hence, to make it work for negative values we have to make little modification in our logic.

Program to find reverse of a negative number using recursion

To find reverse of negative number use the logic for positive number reverse which I mentioned above. In addition check if the number is negative then convert it to positive and then pass it to reverse() function. One more condition you need to check, if number was negative then convert the reversed number to negative.

That's all, let us implement this.

/**
 * 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, numIsNegative;

    /* Input number from user */    
    printf("Enter any number: ");
    scanf("%d", &num);
    
	/* Assign 1 if(num < 0) otherwise 0 */
	numIsNegative = (num < 0);
	
	/* If num is negative, then convert it to positive */
	if(numIsNegative)
		num *= -1;
	
    /* Call the function to reverse number */	
    rev = reverse(num); 
	
	/* If num was negative, then convert the reverse to negative */
	if(numIsNegative)
		rev *= -1;
    
    printf("Reverse of %d = %d", num, rev);
    
    return 0;
}


/**
 * Recursive function to find reverse of any number
 */
int reverse(int num)
{
    // Find total number of digits in num
    int digit = (int) log10(num);
    
    // Base condition
    if(num == 0)
        return 0;
    
    return ((num%10 * pow(10, digit)) + reverse(num/10));
}

Output

Enter any number: 12345
Reverse of 12345 = 54321

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>