Reverse a Number using Stack

Problem Statement:

You will be given an integer. You have to reverse the digits of that integer using Stack and return an integer with the reversed digits.

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

Consider the number 12345. We will extract each digit of this number and insert it into the stack one by one. The way to extract a digit from a number is to calculate the remainder that it leaves upon dividing it by 10. So, the following will be the remainder and the leftover number after extracting a 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).

We tried to discuss Reverse a Number Using Stack. We hope this article gives you a better understanding ofReverse a Number Using Stack, PrepBytes also provides a good collection of Foundation Courses that can help you enhance your coding skills. Want to make sure you ace the interview in one go? Join our Placement Program which will help you get prepared and land your dream job at MNCs. Mentors of Prepbytes are highly experienced and can provide you with basic, in-depth subject knowledge for better understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *