C Program to Reverse a Number using Recursion

This is a C program to find reverse of a number using recursion.

Problem Description

This C program finds the reverse of a number using recursion.

Problem Solution

The following C program using recursion reverses the digits of the number and displays it on the output of the terminal. Eg: 123 becomes 321.

Program/Source Code

Here is the source code of the C program to find the reverse of a number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*  
 * C program to find the reverse of a number using recursion
 */
#include <stdio.h>
#include <math.h>
 
int rev(int, int);
 
int main()
{
    int num, result;
    int length = 0, temp;
 
    printf("Enter an integer number to reverse: ");
    scanf("%d", &num);
    temp = num;
    while (temp != 0)
    {
        length++;
        temp = temp / 10;
    }
    result = rev(num, length);
    printf("The reverse of %d is %d.\n", num, result);
    return 0;
}
 
int rev(int num, int len)
{
    if (len == 1)
    {
        return num;
    }
    else
    {
        return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
    }
}
Program Explanation

In this C program, we are reading the integer number using the ‘num’ variable. Assign the value of ‘num’ variable to ‘temp’ variable. While loop is used to check the condition the value of ‘temp’ variable is not equal to 0, if the condition is true execute the statement divide the value of ‘temp’ variable by 10.

advertisement
advertisement

The result variable is used to call the rev() function by passing ‘num’ and ‘length’ variable value as argument. The function rev() is used to reverse the digits of the number. If else condition statement is used to check the value of ‘len’ variable is equal to 1. If the condition is true execute the statement.

Otherwise, if the condition is false execute the statement. Compute the modulus of the value of ‘num’ variable by 10 integer and multiply the resulted value with 10. Compute the power of the value of ‘len’ variable using pow() function. Add the resulted value ‘num’ variable with 10. Print the reverse of a number using recursion.

Runtime Test Cases
 
$ cc pgm34.c
$ a.out
Enter an integer number to reverse: 1234
The reverse of 1234 is 4321.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.