C Program to Reverse Number Using Recursive Function

Question: Write a program in C to find reverse of a given integer number using recursive function (recursion).

Reversing Number Using Recursion in C


#include<stdio.h>

int reverse(int , int );

int main()
{
    int number, result;
    printf("Enter number: ");
    scanf("%d", &number);
    /* Second argument must be 0 while calling function */
    result = reverse(number, 0);

    printf("Reverse of %d is %d.", number, result);
    return 0;
}

int reverse(int num, int rev)
{
    if(num==0)
        return rev;
    else
        return reverse (num/10, rev*10 + num%10);
}

Output

Enter number: 1234
Reverse of 1234 is 4321.