Reversing a number is a common programming task that often arises in various applications. Whether you are a beginner or an experienced programmer, understanding how to reverse a number in C is a fundamental skill that can enhance your problem-solving abilities. In this article, we will explore the concept of reversing a number in C, discuss different approaches to achieve this task, and provide valuable insights to help you grasp the concept effectively.

## Understanding the Problem

Before diving into the implementation details, let’s first understand what it means to reverse a number. Reversing a number involves rearranging its digits in the opposite order. For example, if we reverse the number 123, we get 321. Similarly, reversing 98765 gives us 56789.

## Approaches to Reverse a Number in C

There are several approaches to reverse a number in C. In this section, we will discuss three commonly used methods:

### 1. Using Arithmetic Operations

The first approach involves using arithmetic operations to reverse a number. Here’s a step-by-step breakdown of this method:

- Initialize a variable to store the reversed number (let’s call it
`reversedNum`

) and set it to 0. - Extract the last digit of the given number using the modulo operator (
`%`

) and add it to`reversedNum`

. - Remove the last digit from the given number using integer division (
`/`

). - Repeat steps 2 and 3 until the given number becomes 0.
- Finally,
`reversedNum`

will contain the reversed number.

Let’s take an example to illustrate this approach. Consider the number 123:

- Initialize
`reversedNum`

to 0. - Extract the last digit of 123, which is 3, and add it to
`reversedNum`

. Now,`reversedNum`

becomes 3. - Remove the last digit from 123, resulting in 12.
- Repeat the above steps until the given number becomes 0.

After repeating the steps, we get the reversed number as 321.

### 2. Using Arrays

The second approach involves using arrays to reverse a number. Here’s how it works:

- Convert the given number into a character array.
- Initialize two pointers, one pointing to the first element of the array and the other pointing to the last element.
- Swap the elements pointed by the two pointers and move the pointers towards each other.
- Repeat step 3 until the pointers meet or cross each other.
- Finally, the array will contain the reversed number.

Let’s take an example to understand this approach. Consider the number 98765:

- Convert 98765 into a character array: [‘9’, ‘8’, ‘7’, ‘6’, ‘5’].
- Initialize the first pointer to the first element (‘9’) and the second pointer to the last element (‘5’).
- Swap ‘9’ and ‘5’, resulting in [‘5’, ‘8’, ‘7’, ‘6’, ‘9’].
- Move the first pointer to the next element (‘8’) and the second pointer to the previous element (‘6’).
- Repeat the above steps until the pointers meet or cross each other.

After repeating the steps, the array will contain the reversed number: [‘5’, ‘6’, ‘7’, ‘8’, ‘9’].

### 3. Using Recursion

The third approach involves using recursion to reverse a number. Here’s how it can be done:

- Define a recursive function that takes the given number as an argument.
- If the given number is less than 10, return it as the reversed number.
- Otherwise, recursively call the function with the given number divided by 10 as the argument.
- Multiply the result of the recursive call by 10 and add the last digit of the given number.
- Finally, the result will be the reversed number.

Let’s take an example to illustrate this approach. Consider the number 456:

- Call the recursive function with 456 as the argument.
- Since 456 is not less than 10, recursively call the function with 45 (456 divided by 10) as the argument.
- Again, since 45 is not less than 10, recursively call the function with 4 (45 divided by 10) as the argument.
- Now, 4 is less than 10, so return 4 as the reversed number.
- Multiply the result of the recursive call (4) by 10 and add the last digit of the given number (6). The result is 46.
- Multiply the result of the previous step (46) by 10 and add the last digit of the given number (5). The final result is 456.

After following the steps, we get the reversed number as 456.

## Implementing the Approaches in C

Now that we have discussed the different approaches to reverse a number in C, let’s implement them in code:

### 1. Using Arithmetic Operations

“`c

#include

int reverseNumber(int num) {

int reversedNum = 0;

while (num != 0) {

int remainder = num % 10;

reversedNum = reversedNum * 10 + remainder;

num /= 10;

}

return reversedNum;

}

int main() {

int num;

printf(“Enter a number: “);

scanf(“%d”, &num);

int reversedNum = reverseNumber(num);

printf(“Reversed number: %dn”, reversedNum);

return 0;

}

“`

### 2. Using Arrays

“`c

#include

#include

void reverseNumber(char* num) {

int length = strlen(num);

int start = 0;

int end = length – 1;