Home » C Examples » C Program to Reverse a Number using Recursion

# C Program to Reverse a Number using Recursion

The program uses recursion to reverse an input number. A function is recursively called to extract each digit from the number and place it in reverse order. The final output is a reversed number. We wrote the program using Dev C++ version 4.9.9.2 on a Windows 7 64-bit system.

You must be familiar with following c programming concept before working on the example.

### Problem Definition

The program receives an input number and reverse each by extracting the digits one at a time from original number n. It starts with two initial conditions. The remainder r = 0 and if n=0 then return 0.

The procedure to reverse the number 567 is shown below. The program does the following tasks to reverse the input number.

1. Extract a digit from the unit position.
2. Each iteration multiply the number with 10 to reverse it.
3. Next extracted digit is added to previously obtained result.
4. After final extraction the number is completely reversed.

Go through each step below to understand the working of the reverse() function.

Step 1: Extract the number 7

``````$r = r + n \% 10$
$r = 0 + 567 \% 10$
$r = 7$``````

Step 2: Change the position of a number from unit place to tenth place.