Table of Contents
ToggleIn 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.
- Multiply the existing number (123) by 10: 123×10=1230
- 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:
- Base Case:
- If the number has only one digit, it’s already reversed. Return the number.
- 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](https://media.teachingbee.in/wp-content/uploads/2024/02/reverse-a-number-using-recusion-output-1024x91.png)
![C Program to Reverse a Number using Recursion Output](https://media.teachingbee.in/wp-content/uploads/2024/02/reverse-a-number-using-recusion-output-1024x91.png)
In the above program,
- The
reverseNumber
function takes an integernum
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:
- First Call (
reverseNumber(123)
):- lastDigit = 3
- remainingDigits = 12
- Recursively call
reverseNumber(12)
- Second Call (
reverseNumber(12)
):- lastDigit = 2
- remainingDigits = 1
- Recursively call
reverseNumber(1)
- Third Call (
reverseNumber(1)
):- Base case reached (num < 10), returns 1
- Back to Second Call:
- Concatenate 1 with lastDigit (2), result = 12
- 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.