Remove trailing zeros from the sum of two numbers ( Using Stack ) | Java 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
import java . io . * ; import java . util . * ; class GFG { static String removeTailing ( int A , int B ) { int N = A + B ; Stack < Character > s = new Stack < Character > ( ) ; String strsum = Integer . toString ( N ) ; for ( int i = 0 ; i < strsum . length ( ) ; i ++ ) { s . push ( strsum . charAt ( i ) ) ; } while ( s . peek ( ) == '0' ) { s . pop ( ) ; } String res = " " ; while ( s . empty ( ) == false ) { res = res + ( char ) s . peek ( ) ; s . pop ( ) ; } StringBuilder str = new StringBuilder ( ) ; str . append ( res ) ; str . reverse ( ) ; return str . toString ( ) ; } public static void main ( String [ ] args ) { int A = 130246 , B = 450164 ; System . out . println ( removeTailing ( A , B ) ) ; } }