Concatenate the answer with smaller value of the topmost elements of both the stacks and then pop that element
if ( stack1 . Peek ( ) < stack2 . Peek ( ) ) { ans = ans * 10 + stack1 . Peek ( ) ; stack1 . Pop ( ) ; } else { ans = ans * 10 + stack2 . Peek ( ) ; stack2 . Pop ( ) ; } }