Skip to content
Related Articles

Related Articles

Improve Article

Program to add two integers of given base

  • Difficulty Level : Medium
  • Last Updated : 11 Jun, 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++




// 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 B
string 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 Code
int 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




// Java implementation to find the
// sum of two integers of base B
class 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

Python3




# Python 3 implementation to find the
# sum of two integers of base B
  
# Function to find the sum of
# two integers of base B
def 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 Call
sum = sumBaseB(a, b, base);
print(sum)
 
# This code is contributed by atul_kumar_shrivastava

C#




// C# implementation to find the
// sum of two integers of base B
using 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

Javascript




<script>
 
// Javascript implementation to find the
// sum of two integers of base B
 
// Function to find the sum of
// two integers of base B
function 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 code
let a, b, sum;
let base_var;
a = "123";
b = "234";
base_var = 6;
 
// Function Call
sum = sumBaseB(a, b, base_var);
document.write(sum + "</br>");
 
// This code is contributed by divyesh072019  
 
</script>
Output: 
401

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :