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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string removeTailing ( int A , int B ) { int N = A + B ; stack < int > s ; string strsum = to_string ( N ) ; for ( int i = 0 ; i < strsum . length ( ) ; i ++ ) { s . push ( strsum [ i ] ) ; } while ( s . top ( ) == '0' ) s . pop ( ) ; string res = " " ; while ( ! s . empty ( ) ) { res = res + char ( s . top ( ) ) ; s . pop ( ) ; } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { int A = 130246 , B = 450164 ; cout << removeTailing ( A , B ) ; return 0 ; }