In: Computer Science
Please, write code in c++. Using iostream and cstring library.
Your friend is the person who does not like any limitations in
the life. And when you said to him that it is totally impossible to
work with integer numbers bigger than 4 294 967 296 in C++
he blamed you in time-wasting during the university study.So to
prove that you hadn't waste 2 months of your life studying C++ in
university you have to solve this issue.
Your task is to write a program that which can add two integer
numbers that are not more than 101000 to each
other.
Your program have to implement void addVeryLongIntegers(char
*a,char *b,char *c) function.
Note. The program have to use pointer.
Input:
First line contains a and b numbers that are not bigger that
101000
Output:
The sum of a and b.
example:
input:
222222222222222222222 111111111111111111111
4958439238923098349024 1
output:
333333333333333333333
4958439238923098349025
C++ code
#include<iostream>
#include<string.h>
#define size 1001
using namespace std;
void addVeryLongIntegers(char *a,char *b,char *c){
int n1 = strlen(a)-1 ;
int n2 = strlen(b)-1;
int sum, carry=0,count=0;
while(n2>=0 && n1>=0){
sum = (*(a + n1) - '0') + (*(b+n2)
- '0') + carry;
carry = sum / 10;
sum = sum % 10;
*(c+count) = sum+'0';
n1--;
n2--;
count++;
}
while(n1>=0){
sum = (*(a + n1) - '0') +
carry;
carry = sum / 10;
sum = sum % 10;
*(c+count) = sum + '0';
n1--;
count++;
}
while(n2>=0){
sum = (*(b+n2) - '0') +
carry;
carry = sum / 10;
sum = sum % 10;
*(c+count) = sum + '0';
n2--;
count++;
}
if (carry !=0) {
*(c+count) = carry + '0';
count++;
}
int i=0,j=count-1;
while(i<j){
char temp = *(c+i);
*(c+i) = *(c+j);
*(c+j) = temp;
i++;
j--;
}
*(c+count) = '\0';
}
int main(){
char a[size];
char b[size];
char c[size];
cin>>a;
cin>>b;
addVeryLongIntegers(a,b,c);
for(int i=0;*(c+i) != '\0'
;i++)cout<<*(c+i);
return 0;
}