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 188.8.131.52 on a Windows 7 64-bit system.
You must be familiar with following c programming concept before working on the example.
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.
- Extract a digit from the unit position.
- Each iteration multiply the number with 10 to reverse it.
- Next extracted digit is added to previously obtained result.
- 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
Step 2: Change the position of a number from unit place to tenth place.