Last Updated on May 11, 2023 by Prepbytes

Reversing a number involves changing the order of the digits in a number so that the last digit becomes the first, the second-to-last digit becomes the second, and so on. For example, the number 1234, when reversed, becomes 4321.

The reverse of a number can be useful in various situations, such as in computer programming, where it may be necessary to manipulate numbers in reverse order. Additionally, reversing a number can help in identifying patterns and relationships between numbers.

**Example:**

**Input:**

`12345`

**Output:**

`54321`

## Algorithm to Reverse a Number

- Take the input from the user
- Perform remainder = number % 10
- Print remainder
- Perform number/10
- Repeat steps 2 and 4 until the input number cannot be further divided.

## Method 1: An Iterative Method to Reverse a Number

In this method, we will use a while loop to reverse a number.

### Code Implementation to Reverse a Number using Iterative Method

#include <iostream> using namespace std; int main() { int number, rev = 0, remainder; cout << "\nEnter a number : "; cin >> number; cout << "\nReversed Number : "; while(number != 0) { remainder = number%10; rev = rev*10 + remainder; number /= 10; } cout << rev << endl; return 0; }

**Output:**

```
Enter a number: 12345
Reversed number: 54321
```

**Time Complexity:** O(log(N))

## Method 2: Recursive Program to Reverse a Number

In this method, we will use a recursive function to apply the reverse a number in which the function call itself from its definition part.

### Algorithm to Reverse a Number Recursively

- Create a reverse(int n), a recursive function of void type.
- Base condition will be : if (n < 10) then print(n) and return.
- Otherwise, print(n%10) and call function reverse(n/10).

### Code Implementation to Reverse a Number Recursively

#include <bits/stdc++.h> using namespace std; int reverse_digits(int num) { static int rev_num = 0; static int base_pos = 1; if(num > 0) { reverse_digits(num/10); rev_num += (num%10)*base_pos; base_pos *= 10; } return rev_num; } int main() { int num; cout << "\nEnter a number: "; cin >> num; cout << "\nReversed number : " << reverse_digits(num); return 0; }

**Output:**

```
Enter a number: 12345
Reversed number: 54321
```

**Conclusion**

In conclusion, reversing a number means changing the order of its digits so that the last digit becomes the first, the second-to-last digit becomes the second, and so on. This process can be useful in various fields such as computer programming, mathematics, and engineering. Reversing a number can help in identifying patterns and relationships between numbers, and it can also be an important step in solving certain types of problems. With the help of programming languages and mathematical algorithms, reversing a number can be done easily and efficiently.

## Frequently Asked Questions

**Q1. What is the reverse of a negative number?**

**Ans.** The reverse of a negative number is obtained by reversing the absolute value of the number and then adding a negative sign to the result.

**Q2. Can a decimal number be reversed?**

**Ans.** Yes, a decimal number can be reversed in the same way as a whole number. You can extract each digit of the decimal number using the modulo operator and divide the number by 10, and then reverse the digits in the same way as a whole number.

**Q3. How can I reverse a number using a programming language?**

**Ans.** Most programming languages have built-in functions or methods for reversing a number or a string. For example, in Python, you can use the [::-1] slicing syntax to reverse a string, and you can convert a number to a string using the str() function. In C++, you can use a loop to extract each digit of the number and reverse them one by one.

**Q4. What are some practical applications of reversing a number?**

**Ans.** Reversing a number can be useful in various applications such as cryptography, data encryption, and data compression. It can also be used in mathematical operations such as finding palindromic numbers or checking if a number is a multiple of 9. Additionally, it can be used in programming challenges and puzzles that require manipulating numbers in reverse order.

**Other C++ Programs**

C++ Program to Add Two Numbers

C++ program for heap sort

Bubble Sort in C++ with Program Code and Example