Addition of two numbers without propagating Carry

Given 2 numbers a and b of same length. The task is to calculate their sum in such a way that when adding two corresponding positions the carry has to be kept with them only instead of propagating to the left.

See the below image for reference:

Examples:



Input: a = 7752 , b = 8834
Output: 151586

Input: a = 123 , b = 456
Output: 579

Approach: First of all, reverse both of the numbers a and b. Now, to generate the resulting sum:

  • Extract digits from both a and b.
  • Calculate sum of digits.
  • If sum of digits is a single digit number, append it directly to the resultant sum.
  • Otherwise, reverse the current calculated digit sum and extract digits from it one by one and append to the resultant sum.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to print sum of 2 numbers
// without propagating carry
int printSum(int a, int b)
{
    int res = 0;
      
    int temp1 = 0, temp2 = 0;
      
    // Reverse a
    while(a)
    {
       temp1 = temp1*10 + (a%10);
       a /= 10;
    }
    a = temp1;
      
    // Reverse b
    while(b)
    {
       temp2 = temp2*10 + (b%10);
       b /= 10;
    }
    b = temp2;
      
    // Generate sum
    // Since length of both a and b are same,
    // take any one of them.
    while(a)
    {   
        // Extract digits from a and b and add
        int sum = (a%10 + b%10);
          
        // If sum is single digit
        if(sum/10 == 0)    
            res = res*10 + sum;
        else
        {
            // If sum is not single digit
            // reverse sum
            temp1 = 0;
            while(sum)
            {
                temp1 = temp1*10 + (sum%10);
                sum /= 10;
            }
            sum = temp1;
              
            // Extract digits from sum and append
            // to result
            while(sum)
            {
                res = res*10 + (sum%10);
                sum /=10;
            }
        }
          
        a/=10;
        b/=10;
    }
      
    return res;
}
  
// Driver code
int main()
{
    int a = 7752, b = 8834;
    cout<<printSum(a, b);
      
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
    // Function to print sum of 2 numbers
    // without propagating carry
    static int printSum(int a, int b) 
    {
        int res = 0;
  
        int temp1 = 0, temp2 = 0;
  
        // Reverse a
        while (a != 0
        {
            temp1 = temp1 * 10 + (a % 10);
            a /= 10;
        }
        a = temp1;
  
        // Reverse b
        while (b != 0
        {
            temp2 = temp2 * 10 + (b % 10);
            b /= 10;
        }
        b = temp2;
  
        // Generate sum
        // Since length of both a and b are same,
        // take any one of them.
        while (a != 0
        {
            // Extract digits from a and b and add
            int sum = (a % 10 + b % 10);
  
            // If sum is single digit
            if (sum / 10 == 0
            {
                res = res * 10 + sum;
            
            else
            {
                // If sum is not single digit
                // reverse sum
                temp1 = 0;
                while (sum != 0)
                {
                    temp1 = temp1 * 10 + (sum % 10);
                    sum /= 10;
                }
                sum = temp1;
  
                // Extract digits from sum and append
                // to result
                while (sum != 0)
                {
                    res = res * 10 + (sum % 10);
                    sum /= 10;
                }
            }
  
            a /= 10;
            b /= 10;
        }
  
        return res;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
  
        int a = 7752, b = 8834;
        System.out.println(printSum(a, b));
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to prsum of 2 numbers 
# without propagating carry 
def printSum(a, b): 
  
    res, temp1, temp2 = 0, 0, 0
      
    # Reverse a 
    while a > 0
      
        temp1 = temp1 * 10 + (a % 10
        a //= 10
      
    a = temp1 
      
    # Reverse b 
    while b > 0
      
        temp2 = temp2 * 10 + (b % 10
        b //= 10
      
    b = temp2 
      
    # Generate sum 
    # Since length of both a and b are same, 
    # take any one of them. 
    while a: 
          
        # Extract digits from a and b and add 
        Sum = a % 10 + b % 10
          
        # If sum is single digit 
        if Sum // 10 == 0
            res = res * 10 + Sum
          
        else:
          
            # If sum is not single digit 
            # reverse sum 
            temp1 = 0
            while Sum > 0
              
                temp1 = temp1 * 10 + (Sum % 10
                Sum //= 10
              
            Sum = temp1 
              
            # Extract digits from sum and 
            # append to result 
            while Sum > 0
              
                res = res * 10 + (Sum % 10
                Sum //= 10
          
        a //= 10
        b //= 10
      
    return res 
  
# Driver code 
if __name__ == "__main__"
  
    a, b = 7752, 8834
    print(printSum(a, b))
      
# This code is contributed 
# by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG
{
  
// Function to print sum of 2 numbers
// without propagating carry
static int printSum(int a, int b)
{
    int res = 0;
      
    int temp1 = 0, temp2 = 0;
      
    // Reverse a
    while(a != 0)
    {
        temp1 = temp1 * 10 + (a % 10);
        a /= 10;
    }
    a = temp1;
      
    // Reverse b
    while(b != 0)
    {
        temp2 = temp2 * 10 + (b % 10);
        b /= 10;
    }
    b = temp2;
      
    // Generate sum
    // Since length of both a and b are same,
    // take any one of them.
    while(a != 0)
    
        // Extract digits from a and b and add
        int sum = (a % 10 + b % 10);
          
        // If sum is single digit
        if(sum / 10 == 0) 
            res = res * 10 + sum;
        else
        {
            // If sum is not single digit
            // reverse sum
            temp1 = 0;
            while(sum != 0)
            {
                temp1 = temp1 * 10 + (sum % 10);
                sum /= 10;
            }
            sum = temp1;
              
            // Extract digits from sum and append
            // to result
            while(sum != 0)
            {
                res = res * 10 + (sum % 10);
                sum /=10;
            }
        }
          
        a /= 10;
        b /= 10;
    }
      
    return res;
}
  
// Driver code
public static void Main()
{
    int a = 7752, b = 8834;
    Console.Write(printSum(a, b));
      
}
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to print sum of 2 numbers
// without propagating carry
function printSum($a, $b
{
    $res = 0;
  
    $temp1 = 0; $temp2 = 0;
  
    // Reverse a
    while ($a != 0) 
    {
        $temp1 = $temp1 * 10 + ($a % 10);
        $a = (int)($a / 10);
    }
    $a = $temp1;
  
    // Reverse b
    while ($b != 0) 
    {
        $temp2 = $temp2 * 10 + ($b % 10);
        $b = (int)($b / 10);
    }
      
    $b = $temp2;
  
    // Generate sum
    // Since length of both a and b are same,
    // take any one of them.
    while ($a != 0) 
    {
        // Extract digits from a and b and add
        $sum = ($a % 10 + $b % 10);
  
        // If sum is single digit
        if ((int)($sum / 10) == 0) 
        {
            $res = $res * 10 + $sum;
        
        else
        {
              
            // If sum is not single digit
            // reverse sum
            $temp1 = 0;
            while ($sum != 0)
            {
                $temp1 = $temp1 * 10 + ($sum % 10);
                $sum = (int)($sum / 10);
            }
            $sum = $temp1;
  
            // Extract digits from sum and append
            // to result
            while ($sum != 0)
            {
                $res = $res * 10 + ($sum % 10);
                $sum = (int)($sum / 10);
            }
        }
  
        $a = (int)($a / 10);
        $b = (int)($b / 10);
    }
  
    return $res;
}
  
// Driver code
$a = 7752; $b = 8834;
echo(printSum($a, $b));
  
// This code contributed by Code_Mech.
?>

chevron_right


Output:

151586

Time Complexity: O(N).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.