Reverse a number using stack | C # program to reverse the number using a stack ; Stack to maintain order of digits ; Function to push digits into stack ; Function to reverse the number ; Function call to push number 's   digits to stack  ; Popping the digits and forming the reversed number ; Return the reversed number formed ; Driver Code ; Function call to reverse number
using System ; using System . Collections . Generic ; class GFG { public static Stack < int > st = new Stack < int > ( ) ; public static void push_digits ( int number ) { while ( number != 0 ) { st . Push ( number % 10 ) ; number = number / 10 ; } } public static int reverse_number ( int number ) { push_digits ( number ) ; int reverse = 0 ; int i = 1 ; while ( st . Count > 0 ) { reverse = reverse + ( st . Peek ( ) * i ) ; st . Pop ( ) ; i = i * 10 ; } return reverse ; } public static void Main ( string [ ] args ) { int number = 39997 ; Console . WriteLine ( reverse_number ( number ) ) ; } }