Addition of two numbers in base 14 in C++

In this tutorial, we will look at how to add two numbers represented in base 14 in C++. The number system that we see the most in our daily lives is the base 10 or ‘decimal’ system. Binary (base 2), Octal (base 8), and Hexadecimal (base 16) number systems are often used in computers for the representation and manipulation of numbers.

In this post, we will first learn about the representation of numbers in a base 14 system.
Next, we will look at the addition of numbers in base 14.
Finally, we will look at the C++ code to implement this.

Understanding numbers in base 14

Numbers in the decimal number system are represented using the digits:
0, 1, 2, 3, 4, 5, 6,  7, 8, and 9.

This means that every whole number can be represented as a pattern of these 10 digits. For example, the number ‘five thousand, one hundred and eighty-seven’ is represented as ‘5187’.

A base 14 number system, (also known as a tetradecimal system), uses 14 digits to represent whole numbers. Technically speaking, we do not use 14 ‘digits’ but rather 14 ‘symbols’:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, and D.

Note that ‘A’ is equivalent to the decimal number ’10’, ‘B’ is ’11’, and so on. Given below are a few numbers in decimal and their equivalent base 14 representation.

Decimal Representation           Base 14 Representation

               9                                              9
               10                                            A
               13                                            D
               14                                            10
               222                                          11C
               5187                                         1C67

Addition of numbers in base 14

The addition of two numbers in base 14 is very similar to the addition of two decimal numbers. We add from right to left while considering carry-overs. In decimal addition, we carry over to the next place if corresponding digits (and a carry if present) add to a value greater than 9. Here, we carry over to the next place if corresponding symbols (and a carry if present) add to a value greater than 13. Shown below is the addition of a few pairs of base 14 numbers

           12 + 14 = 26
           8D3 + 91A = 140D
           233  + BB = 310

C++ implementation and explanation: Add two base 14 numbers

We take in the numbers to be added as strings (say, ‘n1’ and ‘n2’). For simplicity, we assume that the strings entered represent valid base 14 numbers. We use strings rather than character arrays because of the various useful functions that operate on string objects. These functions help us to easily resize, append and reverse strings.

  1. The first step is to ensure that both inputs are of equal length. We do this by appending ‘0’s to the front of the shorter string.
  2. We then declare an empty string to store our final sum (say, ‘s’).
  3. Starting from the rightmost (i.e. unit’s place) of ‘n1’ and ‘n2’, we add symbols while at the same time taking into consideration carry.
  4. The sum of corresponding symbols is stored in ‘s’ and new carry is calculated. In this way ‘s’ will contain the required sum but in reverse.
  5. Finally, we reverse ‘s’ and display.

Addition of two numbers in base 14 in C++

#include <iostream>
#include <vector> 
#include <string>
#include <algorithm>

using namespace std;

void change_lengths(string &n1, string &n2) // Function to append '0's to
                                            // the front of the shorter 
                                            // string
{
  int k = n2.size() - n1.size();
    
  string n3(k, '0');
    
  int i;

  for (i = 0; i < n1.size(); i++)
    n3.push_back(n1[i]);

  n1 = n3;
    
}
void sum(string n1, string n2)
{
  if (n2.size() > n1.size())
    change_lengths(n1, n2);

  else if (n1.size() > n2.size())
    change_lengths(n2, n1);

  string s;
  int i, val1, val2, val3, c = 0; // val1, val2 and val3 hold the 
                                  // numeric values of the symbols of 
                                  // n1, n2 and s respectively.  
                                  // c is the carry
  

  for (i = n1.size() - 1; i >= 0; i--)
  {
    if (n1[i] >= '0' && n1[i] <= '9')
      val1 = n1[i] - '0';

    else if (n1[i] >= 'A' && n1[i] <= 'D')
      val1 = n1[i] - 'A' + 10;

    if (n2[i] >= '0' && n2[i] <= '9')
      val2 = n2[i] - '0';

    else if (n2[i] >= 'A' && n2[i] <= 'D')
      val2 = n2[i] - 'A' + 10;

    val3 = (val1 + val2 + c) % 14; 
    c = (val1 + val2 + c) / 14;

    if (val3 <= 9)
      s.push_back(val3 + '0');

    else
      s.push_back(val3 + 'A' - 10);					
  }

  if (c == 1) //In case there is a final carry
    s.push_back('1');

  reverse(s.begin(), s.end()); // Since sum was initialised
                               // in reverse.

  //cout << "Sum = ";
  cout <<  s;
}

int main()
{
  string n1, n2;

  //cout << "Enter two numbers in base 14\n";
  cin >> n1 >> n2;
  
  sum(n1, n2);
}

Input

8D3 91A

Output

140D

Conclusion

In this tutorial, we saw how to add two numbers in base 14 using C++.
Note: We assumed that the inputs provided are valid base 14 numbers.

Read this if you want to know how to add two numbers without using Arithmetic Operators in C++.

Leave a Reply