C Program To Reverse a Number using Recursion


Lets write a C program to reverse a user input number, using recursive function.

Example: If user input number is 12345, recursive function should return 54321 i.e., the reversed number.

Related Read:
C Program To Reverse a Number
Recursive Functions In C Programming Language

Video Tutorial: C Program To Reverse a Number using Recursion


[youtube https://www.youtube.com/watch?v=yQt27d-OBfk]

YouTube Link: https://www.youtube.com/watch?v=yQt27d-OBfk [Watch the Video In Full Screen.]


Source Code: C Program To Reverse a Number using Recursion

#include<stdio.h>
#include<math.h>

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

int main()
{
    int num, isNegative = 1, result = 0;

    printf("Enter a number to reverse\n");
    scanf("%d", &num);

    isNegative = (num < 0);

    if(isNegative)
        num *= -1;

    result = reverse(num);

    if(isNegative)
        result *= -1;


    printf("Reverse of %d is %d\n", num, result);

    return 0;
}

Output 1:
Enter a number to reverse
12345
Reverse of 12345 is 54321

Output 2:
Enter a number to reverse
-12345
Reverse of -12345 is -54321

Logic To Reverse a Number using Recursion

We ask the user to input a number, and store it inside variable num. If num is negative, then we store 1(true) inside variable isNegative or store 0(false) inside variable isNegative if num is positive.

If isNegative is 1, then we change the value of num to positive and send it as argument to reverse() function.

Inside reverse() function
If num is 0, it returns 0. Else we recursively call reverse() function as follows:

(num%10) * pow(10, (int)log10(num)) + reverse(num/10)

num % 10 gives the last digit in the number.
log10(num) gives the length of the number or the number of digits present in the number. The count starts from 0. Ex: if num = 12345, then log10(num) would give 4.

num/10 reduces the number by 1 digit from right.

pow(10, (int)log10(num)) is used to properly place the reminder value in its decimal place.

Lets assume that user has input 1234 as value of num.

i.e., num = 1234;

Recursive Function Calls – Stacking of calls

numnum % 10num%10*log10(num)num/10
1234reverse(1234)
123444 * 103 = 4000reverse(123)
12333 * 102 = 300reverse(12)
1222 * 101 = 20reverse(1)
111 * 100 = 1reverse(0)

Value Returning – Control Shifting back.

Return ToresultresolveReturn Value
reverse(0)0
reverse(1)1* 100+reverse(0)1 * 1 + 01
reverse(12)2*101+reverse(1)2 * 10 + 121
reverse(123)3*102+reverse(12)3 * 100 + 21321
reverse(1234)4*103+reverse(123)4 * 1000 + 3214321

Note: Whenever there is a call to a function, the function instance(and memory associated with it) gets stored in the memory stack. Once the function returns value to calling function, the control shifts back to the calling function and the memory instance gets popped or deleted out of the memory stack immediately after returning the value.

Source Code: C Program To Reverse a Number using Recursion and Ternary Operator

#include<stdio.h>
#include<math.h>

int reverse(int num)
{
return( (num>0) ?
        ((num%10) * pow(10, (int)log10(num)) + reverse(num/10)) :
         0);
}

int main()
{
    int num, isNegative = 1, result;

    printf("Enter a number to reverse\n");
    scanf("%d", &num);

    isNegative = (num < 0);

    if(isNegative)
        num *= -1;

    result = reverse(num);

    if(isNegative)
    {
        num *= -1;
        result *= -1;
    }


    printf("Reverse of %d is %d\n", num, result);

    return 0;
}

Output 1:
Enter a number to reverse
123
Reverse of 123 is 321

Output 2:
Enter a number to reverse
-123
Reverse of -123 is -321

Know more about ternary operator or conditional operator, watch a separate video tutorial: Ternary Operator / Conditional Operator In C

Source Code: C Program To Reverse a Number using Recursion without using log10() function

#include<stdio.h>
#include<math.h>

int reverse(int num, int len)
{
    if(num)
        return( (num%10) * pow(10, len-1) + reverse(num/10, len-1) );
    else
        return 0;
}

int main()
{
    int num, count = 0, temp;

    printf("Enter a number to reverse\n");
    scanf("%d", &num);

    temp = num;

    while(temp)
    {
        count++;
        temp = temp / 10;
    }

    printf("Reverse of %d is %d\n", num, reverse(num, count));

    return 0;
}

Output 1:
Enter a number to reverse
123456
Reverse of 123456 is 654321

Output 2:
Enter a number to reverse
-123456
Reverse of -123456 is -654321

Logic To Reverse a Number using Recursion without using log10() function

Inside main method itself we calculate the number of digits present in the user entered number and then pass that information to the function reverse() along with the user entered number.

reverse(num, count);

Inside reverse() function
Inside reverse() function, we get the reminder by modulo dividing num by 10. We place this remainder in it’s decimal place by multiplying it with

pow(10, len-1)

where len is the length or the number of digits present in the number.

Next we recursively call reverse() function and pass the value of (num/10) and (len-1).

Finally we combine all these results using below formula and return the value to the calling function recursively, until num is equal to 0:

(num%10) * pow(10, len-1) + reverse(num/10, len-1)

Look at above tables to know the calling function and return values.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

Your email address will not be published. Required fields are marked *