Site icon DataFlair

C Program to Reverse a Number using Recursion

c program to reverse a number

A function can call itself thanks to the effective programming technique known as recursion. This method can be applied to elegantly resolve issues that can be divided into smaller issues. Reversing a number is one such issue that can be resolved recursively by gradually extracting and adding the digits.

Reversing a number simply means changing the order of its digits. For example, reversing 123 would result in 321. Being able to reverse a number programmatically is a common interview question and a good way to get familiar with recursion.

In C, we can leverage recursion to reverse a number with just a few lines of code. The key is to continuously divide the number by 10, extract the last digit using the modulo operator, and accumulate it into the result. This article will walk through this recursive approach step-by-step.

Prerequisites

To follow along with this article, readers should have:

Overview Of Recursion in C

Recursion is a problem-solving method where a function repeatedly calls itself. The characteristics of a recursive function are as follows:

Recursion is useful when a problem can be broken down into simpler sub-problems. Each recursive call works on a smaller sub-problem until the base case is reached.

For example, calculating the factorial of a number lends itself well to a recursive solution:

int factorial(int n) {
  if (n == 0) { // base case
    return 1; 
  }
  
  return n * factorial(n-1); // recursive call
}

Here, the factorial function calls itself with n-1 until n == 0. Recursion provides an elegant solution in this case.

Recursive Approach

The recursive approach is to successively extract the last digit and accumulate it into the result:

For example, to reverse 123:

Here is a recursive pseudocode implementation:

reverse(num)
  if num == 0 
    return 0
  digit = num % 10
  return digit + 10 * reverse(num / 10)

 

Implementation in C

Here is the C implementation of recursively reversing a number:

// Recursive function to reverse a number 
int reverse(int num) {

  // Base case
  if (num == 0)  
    return 0;

  // Extract last digit and append to reversed number 
  int digit = num % 10; 
  int reversed = digit + 10 * reverse(num / 10);

  return reversed;
}

// Driver code
int main() {
  int num = 12345;
  int reversed = reverse(num); 
  printf("Reversed number is: %d", reversed);

  return 0;
}

Output:
Reversed number is: 54321

This recursively extracts and accumulates each digit to build the final reversed number.

Time and Space Complexity

The time complexity of the algorithm is denoted as O(n), where ‘n’ signifies the count of digits in the input. This complexity arises from the fact that the algorithm involves extracting a digit and making a recursive call for each digit in the input, leading to a linear relationship between the execution time and the size of the input.

On the other hand, the space complexity is also O(n). This is primarily due to the space required for the recursion stack, which grows in proportion to the number of recursive calls made by the algorithm. Since the number of recursive calls is directly tied to the size of the input (number of digits), the space complexity is linear relative to the input size.

Conclusion

This article demonstrated how to recursively reverse a number in C – from the concept and algorithm to implementation and testing. The elegance of recursion lies in breaking a problem down into simpler sub-problems.

Mastering recursion in C will equip you with a powerful technique to solve a variety of algorithmic problems. Starting with reversing a number serves as an excellent first step. Readers are encouraged to apply recursion to other problems like computing Fibonacci numbers.

Exit mobile version