C program to find reverse of a number using recursion

Category: C Program

Learn how to write a C program to find the reverse of a number using recursion. This guide covers the concept of recursion, a detailed algorithm, and complete code examples.

Reversing a number is a common task in programming that involves rearranging the digits of the number in the opposite order. One of the efficient ways to achieve this is by using recursion, a technique where a function calls itself. In this article, we'll explore how to write a C program to find the reverse of a number using recursion. We'll explain the concept of recursion, outline the algorithm, and provide a complete C program with detailed explanations.

Understanding Recursion

Recursion is a technique in which a function calls itself to solve a smaller instance of the same problem. It consists of two main parts:

  1. Base Case: The condition under which the recursive function stops calling itself.
  2. Recursive Case: The part where the function calls itself with modified parameters.

Algorithm to Reverse a Number Using Recursion

To reverse a number using recursion, the main idea is to extract the last digit of the number and add it to a reversed version of the remaining digits. This process continues until there are no more digits left.

Here's a step-by-step breakdown:

  1. Extract the Last Digit: Use the modulus operator (%) to get the last digit of the number.
  2. Remove the Last Digit: Use integer division (/) to remove the last digit from the number.
  3. Recursive Call: Pass the remaining number to the recursive function along with the current reverse number multiplied by 10 plus the last digit.
  4. Base Case: When the number becomes 0, return the reverse number.

Write a C program to find reverse of a number using recursion

Here is the complete C program to find the reverse of a number using recursion:

#include <stdio.h>

// Recursive function to find the reverse of a number
int reverseNumber(int num, int reverse) {
    if (num == 0) // Base case
        return reverse;
    else {
        int lastDigit = num % 10; // Extract the last digit
        reverse = reverse * 10 + lastDigit; // Update the reverse number
        return reverseNumber(num / 10, reverse); // Recursive call
    }
}

int main() {
    int number, reverse = 0;

    // Input the number from the user
    printf("Enter a number: ");
    scanf("%d", &number);

    // Call the recursive function
    reverse = reverseNumber(number, 0);

    // Output the reversed number
    printf("The reverse of %d is %d.", number, reverse);

    return 0;
}

Output

Enter a number: 4563
The reverse of 4563 is 3654.

Explanation of the Code

  1. Recursive Function reverseNumber:
    • This function takes two parameters: the original number (num) and the reverse number so far (reverse).
    • Base Case: If num is 0, the function returns reverse, which contains the reversed number.
    • Recursive Case:
      • lastDigit = num % 10; extracts the last digit of num.
      • reverse = reverse * 10 + lastDigit; updates the reverse number by shifting the current digits left and adding the new last digit.
      • return reverseNumber(num / 10, reverse); calls the function recursively with the remaining number and updated reverse.
  2. Main Function main:
    • It prompts the user to enter a number and reads it into the number variable.
    • It calls reverseNumber with the initial reverse value set to 0.
    • Finally, it prints the reversed number.

Example Runs

Example 1

Enter a number: 1234
The reverse of 1234 is 4321.

Example 2

Enter a number: 56789
The reverse of 56789 is 98765.

Recommended Posts