Home » Technical Interview Questions » Stack Interview Questions » Reverse a Number Using Stack

Reverse a Number Using Stack


Reading Time - 4 mins

In reverse a number using stack problem we have given an integer variable representing a number. Print the reverse the given number using stack.

Reverse a Number Using Stack

Example

Input : 12345

Output : 54321

Input : 207

Output : 702

Explanation for Reverse a Number Using Stack

Let number n = 12345

Start traversing and store the digits of the given number in the stack one by one and update the number as number/10.

  • step 1 : n = 1234     stack = 5
  • step 2 : n = 123     stack = 5,4
  • step 3 : n = 12     stack = 5,4,3
  • step 4 : n = 1     stack = 5,4,3,2
  • step 5 : n =      stack = 5,4,3,2,1

After that pop the elements from the stack and form the number as –

reverse = reverse + (value at the top of the stack * i) (where i= 1, update i as i*10 at each step)

Therefore, output = 54321

Algorithm for Reverse a Number Using Stack

  1. Initialize an integer variable representing a number.
  2. Create a stack of the integer type to store the digits of the given number.
  3. Traverse through the length of the given number and while the number is not equal to 0, push the result of the given number mod 10 into the stack, after that divide the given number by 10 at every iteration.
  4. Create an integer variable to store the reverse of the given number and initialize it as 0. Create another integer variable to hold the place value of the digits and initialize it as 1.
  5. Traverse while the stack is not empty i.e. the size of the stack is not equal to 0. Update the integer variable to store the reverse of the given number as the value stored in the reverse variable itself plus the result of the element at the top of the stack multiply by the integer variable of the place value.
  6. Pop/delete the element at the top of the stack and update the integer variable of the place value as the value stored in place value variable itself multiply by 10.
  7. Print the integer variable containing the reverse of the given number.
READ  Reverse a Stack Using Recursion

C++ Program to reverse a number using stack

#include <iostream>
#include <stack>
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; 
}
54321

Java Program to reverse a number using stack

import java.util.Stack; 
  
class ReverseDigits{ 
    
    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));
        
    } 
}
54321

Complexity Analysis

Time Complexity: O(log n) where n is the length of the given number.

Auxiliary Space: O(log n) because we used log n extra space to reverse the digits of the given number.

References

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions