Reverse a number using stack

1.8

Given a number , write a program to reverse this number using stack.

Examples:

Input : 365
Output : 563

Input : 6899
Output : 9986

We have already discussed the simple method to reverse a number in this post. In this post we will discuss about how to reverse a number using stack.

The idea to do this is to extract digits of the number and push the digits on to a stack. Once all of the digits of the number are pushed to the stack, we will start poping the contents of stack one by one and form a number.
As stack is a LIFO data structure, digits of the newly formed number will be in reverse order.

Below is the C++ implementation of above idea:

C++

// CPP program to reverse the number 
// using a stack

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

// Stack to maintain order of digits
stack <int> st;

// Function to push digits into stack
void push_digits(int number)
{
    while (number != 0) 
    {
        st.push(number % 10);
        number = number / 10;
    }
}

// Function to reverse the number
int reverse_number(int number)
{
    // Function call to push number's 
    // digits to stack
    push_digits(number);
    
    int reverse = 0;
    int i = 1;
    
    // Popping the digits and forming 
    // the reversed number
    while (!st.empty()) 
    {
        reverse = reverse + (st.top() * i);
        st.pop();
        i = i * 10;
    }
    
    // Return the reversed number formed
    return reverse;
}

// Driver program to test above function
int main()
{
    int number = 39997;
    
    // Function call to reverse number
    cout << reverse_number(number);
    
    return 0;
}

Java

// Java program to reverse the number
// using a stack
import java.util.Stack;

public class GFG
{
    // Stack to maintain order of digits
    static Stack<Integer> st= new Stack<>();

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

    // Function to reverse the number
    static int reverse_number(int number)
    {
        // Function call to push number's
        // digits to stack
        push_digits(number);
        int reverse = 0;
        int i = 1;

        // Popping the digits and forming
        // the reversed number
        while (!st.isEmpty())
        {
            reverse = reverse + (st.peek() * i);
            st.pop();
            i = i * 10;
        }

        // Return the reversed number formed
        return reverse;
    }

    // Driver program to test above function
    public static void main(String[] args)
    {
        int number = 39997;
        System.out.println(reverse_number(number));
    }
}
// This code is contributed by Sumit Ghosh


Output:

79993

Time Complexity: O( logN )
Auxiliary Space: O( logN ), Where N is the input number.

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.8 Average Difficulty : 1.8/5.0
Based on 6 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.