How to add big integer in C++? Add using string

Big integer addition using string

Some of the fundamental mathematical operations that we use frequently are addition, subtraction, multiplication, and division. When the size of the number is small it is very easy to perform those operations in any programming language. You can make use of primitive data types to store and perform those operations. But when the size of the number increases drastically then it is not possible to perform those operations using the simple integer(4 bytes) data types or long long int(8 bytes) data types. So in this article, I will show you how to add big integers in C++.

Sum of two big integers using the string in C++

There are many ways to perform the addition of big integers. The one that I am going to show is using the string class. There are no inbuilt string data types in C++. So you have to import a string class in order to use it. You just have to add “#include<string>” in your code in order to use it.

Some of the string operations used in the below program code.

  1. getline(): used to get string user input as a stream of characters.
  2. length(): used to get the length of the string.

You can concatenate the two strings using the plus(+) operator.

Pseudocode of adding two big integer

Make sure that the two string is of the same size. If the size of the two integers is of a different size then append the required number of zeros in front of the numbers to make it of the same size.

//The x and y is of same size
//n is the size of x and y
Algorithm addBigInt(x,y):
  carry = 0
  for i, n to 0:
    sum = x[i] + x[i] + carry
    if sum > 10:
      result = (sum%10) + result
      carry = sum/10
    else 
      result = sum + result
      carry = 0
  if carry not zero result = carry + result
  print result

Program code to add two big integers in C++

//bigInteger.cpp file name
#include<iostream>
#include<string>
using namespace std;
string appendZeros(string, int);
void addBigInt(string, string);

int main(){
	string x,y;
	getline(cin, x);
	getline(cin, y);
	if(x.length() > y.length()){
		y = appendZeros(y, x.length() - y.length());
	}
	if(x.length() < y.length()){
		x = appendZeros(x, y.length() - x.length());
	}
	
	addBigInt(x,y);
	return 0;
}

string appendZeros(string str, int n){
	while(n!=0){
		str = "0" + str;
		n--;
	}
	return str;
}

void addBigInt(string x, string y){
	string result = "";
	int sum, carry=0;
	for(int i=x.length()-1; i>=0; i--){
		sum = (int)x[i]-'0' + (int)y[i]-'0' + carry;
		if(sum > 9){
			result = to_string(sum%10) + result;
			carry = sum/10;
		}else{
			result = to_string(sum) + result;
			carry = 0;
		}
	}

	if(carry != 0){
		result = to_string(carry) + result;
	}

	cout << "sum = " << result << endl;
}

Compile: g++ -o bigInteger bigInteger.cpp
Run: ./bigInteger

Time Complexity of adding two big integers using string




The time complexity of adding two big integers using string is O(n). We iterate through the entire length of the big integer and add it. We make use of the index to access the value of the string at a specific location.

Conclusion

How to add big integers in C++? The solution is a string. Since the primitive data type is not able to store the value of a big integer. As I said that the big integer addition can be done in many ways. One other way is using the array. I have chosen the string over an array. If you use an array then you should know the length of the big integer so that you can declare the required size of the array. But with string, this can be overcome.

The same logic can be used across other programming languages. If it is not able to store and perform the addition using primitive data types. Feel free to drop a comment in the comment section below if you have any doubt.

Keep learning!…

Leave a Comment

Your email address will not be published. Required fields are marked *