TeachingBee

C Program to Reverse a Number using Recursion

Reverse a Number using Recursion IN C

In this blog post, we will see how to write C Program to Reverse a Number using Recursion with detailed explanation.

Before we start let’s understand few basic.

How to Fetch last digit of number?

To fetch the last digit of a number, you can use the modulo operation (%). The modulo operation returns the remainder when the number is divided by another. Here’s how you can fetch the last digit with an example:

Example: Let’s take the number 9876.

  • The last digit is obtained by taking the number modulo 10.
  • 9876mod  10=6
  • Therefore, the last digit of 9876 is 6.

How to form a number?

A number can be formed by multiplying by 10 involves appending a digit to the existing number by placing it in a position that is one place to the left. Here’s an example:

Example: Consider you have the number 123, and you want to add the digit 4 to it.

  1. Multiply the existing number (123) by 10: 123×10=1230
  2. Add the new digit (4) to the result: 1230+4=12341230+4=1234.

We will use above concepts to reverse a number in C.

To write a C Program to Reverse a Number using Recursion you can follow this algorithm and corresponding pseudo code.

Algorithm:

  1. Base Case:
    • If the number has only one digit, it’s already reversed. Return the number.
  2. Recursive Case:
    • Extract the last digit of the number by taking the modulo (remainder) when divided by 10.
    • Multiply the current reversed number by 10 and add the extracted digit.
    • Call the reverse function with the remaining digits.

C Program to Reverse a Number using Recursion

#include<stdio.h>

// Function to reverse a number using recursion
int reverseNumber(int num) {
    // Base Case
    if (num < 10) {
        return num;
    }

    // Recursive Case
    int lastDigit = num % 10;
    int remainingDigits = num / 10;
    int reversedNumber = reverseNumber(remainingDigits) * 10 + lastDigit;

    return reversedNumber;
}

int main() {
    // Example
    int inputNumber = 123;
    int result = reverseNumber(inputNumber);

    // Output the reversed number
    printf("Reversed Number: %d\n", result);

    return 0;
}

Output

C Program to Reverse a Number using Recursion Output

In the above program,

  • The reverseNumber function takes an integer num as input and reverses it using recursion.
  • In the base case, if num has only one digit (less than 10), it returns the number as it is already reversed.
  • In the recursive case, it extracts the last digit (lastDigit) and the remaining digits (remainingDigits).
  • The function then calls itself with the remaining digits (reverseNumber(remainingDigits)).
  • Finally, it concatenates the reversed result with the last digit to build the reversed number.

The execution flow for the input value 123 would unfold as follows:

  1. First Call (reverseNumber(123)):
    • lastDigit = 3
    • remainingDigits = 12
    • Recursively call reverseNumber(12)
  2. Second Call (reverseNumber(12)):
    • lastDigit = 2
    • remainingDigits = 1
    • Recursively call reverseNumber(1)
  3. Third Call (reverseNumber(1)):
    • Base case reached (num < 10), returns 1
  4. Back to Second Call:
    • Concatenate 1 with lastDigit (2), result = 12
  5. Back to First Call:
    • Concatenate 12 with lastDigit (3), result = 123

The final reversed number for the example input 123 is 123.

If you liked this post you can checkout other C Programs.

FAQ

How does the C program for reversing a number using recursion work?

The program recursively extracts the last digit, builds the reversed number, and continues the process until the entire number is reversed.

What is the base case in the recursive function for reversing a number in C?

The base case checks if the number has only one digit (less than 10) and directly returns the number, stopping the recursion.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In