  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Reverse a Number using Stack

Last Updated on July 24, 2023 by Mayank Dham A stack is a type of linear data structure that follows a specific order for performing operations. The sequence can either be Last In First Out (LIFO) or First In Last Out (FILO). Let’s discuss the problem statement – Given an integer, your task is to use a Stack data structure to reverse its digits and return the resulting integer with the digits reversed.

### Example

If the number 123 is given to you as the input, the output will be 321. It is compulsory to use a Stack to reverse the number.

### Approach on How to Reverse a number using Stack Data Structure

Let’s take the number 12345 as an example. To extract each digit of this number and insert it into the stack, we calculate the remainder obtained by dividing the number by 10. This remainder represents the last digit, and the leftover number is the result after removing the last digit. So, we have to do this till the number is greater than 0. The following images show how we are extracting the digits from the number and inserting them into the Stack.

![]()   So, now when the number has become 0, we will stop our procedure. So, we have the digits of the number in the Stack. Now, we have to create an integer that will be the reverse of the input. So, for that, let us take an integer “reverse” and initialize it to 0 and a variable “power” initialized to 1 as shown below. Now, we will pop a digit from the stack, and add it to our reverse by multiplying it with power. The variable power will be multiplied by 10 in every iteration. For instance, let us pop 1 from the stack first. Now, in the next iteration, the value of reverse will be 1 and the value of power will be 10. So, let us extract the digit again and add it to reverse by multiplying it with power. So, the reverse will now be 21 and the power will be 100. So, we again pop from the Stack, and this time we get the digit 3. We again, add the digit to the reverse by multiplying it with power. So, similarly, we will perform these operations till the Stack is empty. The remaining 2 iterations are shown below.  After all these iterations, we have got the reversed number. So, now that we have understood the procedure, let us write the code for the same.

### Code Implementation

```#include <bits/stdc++.h>
using namespace std;

stack <int> st;

void push_digits(int number)
{
while (number != 0)
{
st.push(number % 10);
number = number / 10;
}
}

int reverse_number(int number)
{
push_digits(number);

int reverse = 0;
int i = 1;

while (!st.empty())
{
reverse = reverse + (st.top() * i);
st.pop();
i = i * 10;
}

return reverse;
}

int main()
{
int number = 12345;
cout << reverse_number(number);

return 0;
}
```
```import java.util.*;

public class Main
{
static Stack<Integer> st= new Stack<>();

static void push_digits(int number)
{
while(number != 0)
{
st.push(number % 10);
number = number / 10;
}
}

static int reverse_number(int number)
{
push_digits(number);
int reverse = 0;
int i = 1;

while (!st.isEmpty())
{
reverse = reverse + (st.peek() * i);
st.pop();
i = i * 10;
}

return reverse;
}

public static void main(String[] args)
{
int number = 12345;
System.out.println(reverse_number(number));
}
}
```

Time Complexity: The time complexity of this approach is O(log10N) as we are extracting the digits of a number N by dividing it by 10 every time.

Space Complexity: The space complexity is dependent on the number of digits as all the digits are inserted in the stack. Since there are log10N digits, the space complexity also becomes O(log10N).

Conclusion
Reverse a number using stack is a straightforward process that involves extracting each digit of the number and inserting it into the stack one by one. The Last In First Out (LIFO) property of the stack ensures that the digits are reversed in the correct order. This method offers a time complexity of O(log10 N) as we extract digits by dividing the number by 10 at each step and a space complexity of O(log10 N) since all the digits are stored in the stack.

## Frequently Asked Questions (FAQs) related to how to reverse a number using stack

Below are some FAQs related to how to reverse a number using Stack Data Structure:

Q1. Why use a stack to reverse a number?
Using a stack ensures that the digits are reversed in the correct order, following the Last In First Out (LIFO) property. This approach is straightforward and allows us to reverse numbers of any length.

Q2. Can we reverse a negative number using a stack?
Yes, this approach can handle negative numbers as well. The negative sign is preserved during the reversal process.

Q3. Is there an alternative approach to reverse a number without using a stack?
Yes, there are other approaches to reverse a number without using a stack. One such approach is to convert the number to a string, reverse the string, and convert it back to an integer. However, the stack-based method is efficient and does not involve string operations.

Q4. Can this approach be used to reverse floating-point numbers or non-integer numbers?
The stack-based approach is designed to reverse the digits of an integer number. It may not work directly for floating-point or non-integer numbers. However, with some modifications, you can adapt this method to handle decimal numbers.

Q5. What is the limitation of this approach?
The stack-based approach might not be the most memory-efficient for extremely large numbers with many digits, as it requires storing all the digits in the stack.

Q6. Can we reverse a number without using any additional data structure?
Yes, you can reverse a number without using any additional data structure. You can perform mathematical operations to achieve the reversal by manipulating the digits of the number.