C Program to find 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.

  1. /*
  2.  * C program to find the reverse of a number using recursion
  3.  */
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. int rev(int, int);
  8.  
  9. int main()
  10. {
  11.     int num, result;
  12.     int length = 0, temp;
  13.  
  14.     printf("Enter an integer number to reverse: ");
  15.     scanf("%d", &num);
  16.     temp = num;
  17.     while (temp != 0)
  18.     {
  19.         length++;
  20.         temp = temp / 10;
  21.     }
  22.     result = rev(num, length);
  23.     printf("The reverse of %d is %d.\n", num, result);
  24.     return 0;
  25. }
  26.  
  27. int rev(int num, int len)
  28. {
  29.     if (len == 1)
  30.     {
  31.         return num;
  32.     }
  33.     else
  34.     {
  35.         return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));
  36.     }
  37. }


$ 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 Reference 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.

If you liked this C Program, kindly share, recommend or like below!

About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







Sanfoundry Internships
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry