Reverse a number using stack

28 Apr, 2022
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 popping 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 implementation of above idea:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to reverse the number// using a stack #include <bits/stdc++.h>using namespace std; // Stack to maintain order of digitsstack <int> st; // Function to push digits into stackvoid push_digits(int number){    while (number != 0)    {        st.push(number % 10);        number = number / 10;    }} // Function to reverse the numberint 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 functionint main(){    int number = 39997;         // Function call to reverse number    cout << reverse_number(number);         return 0;}
// Java program to reverse the number// using a stackimport 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
# Python3 program to reverse the# number using a stack # Stack to maintain order of digitsst = []; # Function to push digits into stackdef push_digits(number):     while (number != 0):        st.append(number % 10);        number = int(number / 10); # Function to reverse the numberdef reverse_number(number):         # Function call to push number's    # digits to stack    push_digits(number);         reverse = 0;    i = 1;         # Popping the digits and forming    # the reversed number    while (len(st) > 0):        reverse = reverse + (st[len(st) - 1] * i);        st.pop();        i = i * 10;         # Return the reversed number formed    return reverse; # Driver Codenumber = 39997; # Function call to reverse numberprint(reverse_number(number)); # This code is contributed by mits
// C# program to reverse the number// using a stackusing System;using System.Collections.Generic; class GFG{// Stack to maintain order of digitspublic static Stack<int> st = new Stack<int>(); // Function to push digits into stackpublic static void push_digits(int number){    while (number != 0)    {        st.Push(number % 10);        number = number / 10;    }} // Function to reverse the numberpublic 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.Count > 0)    {        reverse = reverse + (st.Peek() * i);        st.Pop();        i = i * 10;    }     // Return the reversed number formed    return reverse;} // Driver Codepublic static void Main(string[] args){    int number = 39997;    Console.WriteLine(reverse_number(number));}} // This code is contributed by Shrikant13
<?php// PHP program to reverse the number// using a stack // Stack to maintain order of digits$st = array(); // Function to push digits into stackfunction push_digits($number){    global $st;    while ($number != 0)    {        array_push($st, $number % 10);        $number = (int)($number / 10);    }} // Function to reverse the numberfunction reverse_number($number){    global $st;         // Function call to push number's    // digits to stack    push_digits($number);         $reverse = 0;    $i = 1;         // Popping the digits and forming    // the reversed number    while (!empty($st))    {        $reverse = $reverse +                  ($st[count($st) - 1] * $i);        array_pop($st);        $i = $i * 10;    }         // Return the reversed number formed    return $reverse;} // Driver Code$number = 39997; // Function call to reverse numberecho reverse_number($number); // This code is contributed by mits?>
<script> // JavaScript program for the above approach         // Stack to maintain order of digits        let st = [];         // Function to push digits into stack        function push_digits(number)        {            while (number != 0)            {                st.push(number % 10);                number = Math.floor(number / 10);            }        }         // Function to reverse the number        function reverse_number(number)        {                     // Function call to push number's            // digits to stack            push_digits(number);             let reverse = 0;            let i = 1;             // Popping the digits and forming            // the reversed number            while (st.length != 0) {                reverse = reverse + (st[st.length - 1] * i);                st.pop();                i = i * 10;            }             // Return the reversed number formed            return reverse;        }         // Driver program to test above function        let number = 39997;         // Function call to reverse number        document.write(reverse_number(number)); // This code is contributed by Potta Lokesh</script>
Output:  
79993
Time Complexity: O( logN ) Auxiliary Space: O( logN ), Where N is the input number. 
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=JesKFC4na5w" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
shrikanth13
Mithun Kumar
Akanksha_Rai
lokeshpotta20
simmytarika5
number-digits
Mathematical
Stack
Mathematical
Stack
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays
Stack Data Structure (Introduction and Program)
Stack in Python
Stack Class in Java
Check for Balanced Brackets in an expression (well-formedness) using Stack
Introduction to Data Structures