VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-trailing-zeros-from-the-sum-of-two-numbers-using-stack/

⇱ Remove trailing zeros from the sum of two numbers ( Using Stack ) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove trailing zeros from the sum of two numbers ( Using Stack )

Last Updated : 23 Jul, 2025

Given two numbers A and B, the task is to remove the trailing zeros present in the sum of the two given numbers using a stack.

Examples:

Input: A = 124, B = 186
Output: 31
Explanation: Sum of A and B is 310. Removing the trailing zeros modifies the sum to 31.

Input: A=130246, B= 450164
Output : 58041

Approach: The given problem can be solved using the string and stack data structures. Follow the steps below to solve the problem:

Below is the implementation of the above approach:


Output: 
58041

 

Time Complexity: O(len(A + B))
Auxiliary Space: O(len(A + B))


 

Comment