Remove trailing zeros from the sum of two numbers ( Using Stack ) | C # program for the above approach ; Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0' ; Pop the top element ; Stores the resultant number without tailing 0 's ; While s is not empty ; Append top element of S in res ; Pop the top element of S ; Reverse the string res ; Driver Code ; Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static string removeTailing ( int A , int B ) { int N = A + B ; Stack < char > s = new Stack < char > ( ) ; string strsum = N . ToString ( ) ; for ( int i = 0 ; i < strsum . Length ; i ++ ) { s . Push ( strsum [ i ] ) ; } while ( s . Peek ( ) == '0' ) { s . Pop ( ) ; } string res = " " ; while ( s . Count != 0 ) { res = res + ( char ) s . Peek ( ) ; s . Pop ( ) ; } char [ ] str = res . ToCharArray ( ) ; Array . Reverse ( str ) ; return new string ( str ) ; } static public void Main ( ) { int A = 130246 , B = 450164 ; Console . WriteLine ( removeTailing ( A , B ) ) ; } }