Adding base n numbers

Data StructureAlgorithmsMisc Algorithms

In this problem, two numbers are given. The base of those numbers is n. We have to find the result of those number after addition in base n also.

At first, the numbers are converted into decimal numbers. From the decimal values, we can simply add them. Finally, the numbers are converted to base n number again.

The n base numbers are given as a string, because for those numbers, which have base greater than 9, it may contain some alphabets to represent numbers, like hexadecimal numbers, there are 6 letters (A-F).

Input and Output

The base of a number system: 16
First number 2C
Second number 5F
The result of addition is: 8B


baseNtoDec(number, base)

Input − The number string of base N, the value of base N.

Output − The Decimal equivalent of the number in base N.

   len := length of number
   power := 1
   num := 0

   for i := len -1 down to 0, do
      if number[i] >= base, then
         return invalid number
      num := num + number[i] * power
      power := power * base

   return num

decToBaseN(dec, base)

Input: The decimal number, the base N to convert a decimal number to that base.

Output: The number string of base N.

   while dec > 0, do
      res := concatenate (dec mod base) with res
      dec := dec / base

   reverse the result
   return res

addBaseN(num1, num2, base)

Input: Two numbers in base N, the value of base N.

Output: The number after addition in base N.

   dec1 := baseNtoDec(num1, base)
   dec2 := baseNtoDec(num2, base)
   sum := decToBaseN(dec1 + dec2, base)
   return sum


using namespace std;

int getVal(char c) {
   if(c >= '0' && c<='9')
      return int(c-'0');    //decimal value of given number
      return int(c-'A'+10);    //for Alphanumeric numbers

char revVal(int n) {
   if(n >= 0 && n <=9)
      return char(n+'0');    //character value of given number
      return char(n+'A'-10);    //for Alphanumeric numbers, get alphabet from decimal

int baseNtoDec(string number, int base) {
   int len = number.size();
   int power = 1;
   int num = 0;

   for(int i = len-1; i>= 0; i--) {    //from last digit to first digit
      if(getVal(number[i]) >= base)
         return INT_MIN;    //when a digit is >= base, return -ve infinity as error
      num += getVal(number[i])*power;
      power = power*base;
   return num;

string decToBaseN(int dec, int base) {
   string res = "";    //empty string
   while(dec > 0) {
      res += revVal(dec%base);
      dec /= base;

   reverse(res.begin(), res.end());    //reverse the string to get final answer
   return res;

int main() {
   int base;
   string num1, num2, sum;
   cout << "Enter Base: "; cin >> base;
   cout << "Enter first number in base "<<base<<": ";cin >> num1;
   cout << "Enter second number in base "<<base<<": ";cin >> num2;
   sum = decToBaseN((baseNtoDec(num1, base) + baseNtoDec(num2, base)), base);
   cout << "The result of addition is: " << sum;


Enter Base: 16
Enter first number in base 16: 2C
Enter second number in base 16: 5F
The result of addition is: 8B
Published on 12-Jul-2018 10:37:04