Program to add two integers of given base - GeeksforGeeks

25 Nov, 2021
Given three integers X, Y and B, where X and Y are Base-B integers. The task is to find the sum of integers X and Y.
Examples: 
Input: X = 123, Y = 234, B = 6
Output: 401
Explanation:
Sum of two integers in base 6 - 
     1 1   
     1 2 3
+    2 3 4
-------------
     4 0 1

Input: X = 546, Y = 248 B = 9
Output: 805
Explanation:
Sum of two integers in base 9 - 
     1 1
     5 4 6
+    2 4 8
-------------
     8 0 5   
Approach: The idea is to use the fact that whenever two digits of the numbers are added, then the place value will be the modulo of the sum of digits by the base whereas carry will be the integer division of the sum of digits by base. i.e.  
Let two digits of the number be D1 and D2 -
Place Value = (D1 + D2) % B

Carry = (D1 + D2) / B
Similarly, Add every digit from the last to get the desired result.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation to find the// sum of two integers of base B #include <bits/stdc++.h> using namespace std; // Function to find the sum of// two integers of base Bstring sumBaseB(string a,      string b, int base){    int len_a, len_b;     len_a = a.size();    len_b = b.size();     string sum, s;    s = "";    sum = "";     int diff;    diff = abs(len_a - len_b);         // Padding 0 in front of the    // number to make both numbers equal    for (int i = 1; i <= diff; i++)        s += "0";         // Condition to check if the strings    // have lengths mis-match    if (len_a < len_b)        a = s + a;    else        b = s + b;     int curr, carry = 0;         // Loop to find the find the sum    // of two integers of base B    for (int i = max(len_a, len_b) - 1;                           i > -1; i--) {                 // Current Place value for        // the resultant sum        curr = carry + (a[i] - '0') +                       (b[i] - '0');         // Update carry        carry = curr / base;         // Find current digit        curr = curr % base;         // Update sum result        sum = (char)(curr + '0') + sum;    }    if (carry > 0)        sum = (char)(carry + '0') + sum;    return sum;} // Driver Codeint main(){    string a, b, sum;    int base;    a = "123";    b = "234";    base = 6;         // Function Call    sum = sumBaseB(a, b, base);    cout << sum << endl;    return 0;}
// Java implementation to find the// sum of two integers of base Bclass GFG {     // Function to find the sum of    // two integers of base B    static String sumBaseB(String a, String b, int base)    {        int len_a, len_b;             len_a = a.length();        len_b = b.length();             String sum, s;        s = "";        sum = "";             int diff;        diff = Math.abs(len_a - len_b);                 // Padding 0 in front of the        // number to make both numbers equal        for (int i = 1; i <= diff; i++)            s += "0";                 // Condition to check if the strings        // have lengths mis-match        if (len_a < len_b)            a = s + a;        else            b = s + b;             int curr, carry = 0;                 // Loop to find the find the sum        // of two integers of base B        for (int i = Math.max(len_a, len_b) - 1;                            i > -1; i--) {                         // Current Place value for            // the resultant sum            curr = carry + (a.charAt(i) - '0') +                        (b.charAt(i) - '0');                 // Update carry            carry = curr / base;                 // Find current digit            curr = curr % base;                 // Update sum result            sum = (char)(curr + '0') + sum;        }        if (carry > 0)            sum = (char)(carry + '0') + sum;        return sum;    }         // Driver Code    public static void main (String[] args)    {        String a, b, sum;        int base;        a = "123";        b = "234";        base = 6;                 // Function Call        sum = sumBaseB(a, b, base);        System.out.println(sum);    }} // This code is contributed by AnkitRai01
# Python 3 implementation to find the# sum of two integers of base B  # Function to find the sum of# two integers of base Bdef sumBaseB(a,b,base):      len_a = len(a)    len_b = len(b)     s = "";    sum = "";      diff = abs(len_a - len_b);          # Padding 0 in front of the    # number to make both numbers equal    for i in range(1,diff+1):        s += "0"          # Condition to check if the strings    # have lengths mis-match    if (len_a < len_b):        a = s + a    else:        b = s + b;      carry = 0;          # Loop to find the find the sum    # of two integers of base B    for i in range(max(len_a, len_b) - 1,-1,-1):                  # Current Place value for        # the resultant sum        curr = carry + (ord(a[i]) -ord('0')) +( ord(b[i]) - ord('0'));          # Update carry        carry = curr // base          # Find current digit        curr = curr % base;          # Update sum result        sum = chr(curr + ord('0')) + sum             if (carry > 0):        sum = chr(carry + ord('0')) + sum;    return sum  # Driver Code a = "123"b = "234"base = 6      # Function Callsum = sumBaseB(a, b, base);print(sum) # This code is contributed by atul_kumar_shrivastava
// C# implementation to find the// sum of two integers of base Busing System; class GFG {     // Function to find the sum of    // two integers of base B    static string sumBaseB(string a, string b, int base_var)    {        int len_a, len_b;             len_a = a.Length;        len_b = b.Length;             string sum, s;        s = "";        sum = "";             int diff;        diff = Math.Abs(len_a - len_b);                 // Padding 0 in front of the        // number to make both numbers equal        for (int i = 1; i <= diff; i++)            s += "0";                 // Condition to check if the strings        // have lengths mis-match        if (len_a < len_b)            a = s + a;        else            b = s + b;             int curr, carry = 0;                 // Loop to find the find the sum        // of two integers of base B        for (int i = Math.Max(len_a, len_b) - 1;                            i > -1; i--) {                         // Current Place value for            // the resultant sum            curr = carry + (a[i] - '0') +                        (b[i] - '0');                 // Update carry            carry = curr / base_var;                 // Find current digit            curr = curr % base_var;                 // Update sum result            sum = (char)(curr + '0') + sum;        }        if (carry > 0)            sum = (char)(carry + '0') + sum;        return sum;    }         // Driver Code    public static void Main (string[] args)    {        string a, b, sum;        int base_var;        a = "123";        b = "234";        base_var = 6;                 // Function Call        sum = sumBaseB(a, b, base_var);        Console.WriteLine(sum);    }} // This code is contributed by AnkitRai01
<script> // Javascript implementation to find the// sum of two integers of base B // Function to find the sum of// two integers of base Bfunction sumBaseB(a, b, base_var){    let len_a, len_b;       len_a = a.length;    len_b = b.length;       let sum, s;    s = "";    sum = "";       let diff;    diff = Math.abs(len_a - len_b);           // Padding 0 in front of the    // number to make both numbers equal    for(let i = 1; i <= diff; i++)        s += "0";           // Condition to check if the strings    // have lengths mis-match    if (len_a < len_b)        a = s + a;    else        b = s + b;       let curr, carry = 0;           // Loop to find the find the sum    // of two integers of base B    for(let i = Math.max(len_a, len_b) - 1;            i > -1; i--)    {                 // Current Place value for        // the resultant sum        curr = carry + (a[i].charCodeAt() -                         '0'.charCodeAt()) +                       (b[i].charCodeAt() -                         '0'.charCodeAt());           // Update carry        carry = parseInt(curr / base_var, 10);           // Find current digit        curr = curr % base_var;           // Update sum result        sum = String.fromCharCode(            curr + '0'.charCodeAt()) + sum;    }    if (carry > 0)        sum = String.fromCharCode(            carry + '0'.charCodeAt()) + sum;                 return sum;} // Driver codelet a, b, sum;let base_var;a = "123";b = "234";base_var = 6; // Function Callsum = sumBaseB(a, b, base_var);document.write(sum + "</br>"); // This code is contributed by divyesh072019   </script>
401
 
Time Complexity: O(len_a – len_b)
Auxiliary Space: O(1)
Atul_kumar_Shrivastava
ankthon
divyesh072019
souravmahato348
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair