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

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

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.

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)); } }

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.

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.

$ 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 find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice Computer Science MCQs
- Practice BCA MCQs