Question

In: Computer Science

Please, write code in c++. Using iostream and cstring library. Your friend is the person who...

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

Solutions

Expert Solution

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;
}


Related Solutions

A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using...
A C++ question: I want to indent the code of this C++ program: #include<iostream> #include<cstring> using namespace std; int lastIndexOf(char *s, char target) { int n=strlen(s); for(int i=n-1;i>=0;i--) { if(s[i]==target) { return i; } } return -1; } void reverse(char *s) { int n=strlen(s); int i=0,j=n-1; while(i<=j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; i++; j--; } return; } int replace(char *s, char target, char replacementChar) { int len=strlen(s); int total=0; for(int i=0;i<len;i++) { if(s[i]==target) { s[i]=replacementChar; total+=1; } } return total;...
Please write code in c++. Use iostream (and any string library if you need it). Create...
Please write code in c++. Use iostream (and any string library if you need it). Create s structure plane : First line contains n(0 < n < 1001). Then n lines inputed in given format:   First - ID[int type]   Second - FromLocation[char*]   Third - ToLocation[char*]   Fourth - DepartureTime[char*] Output: Sorted list of planes should be in UPPER CASE. Example of input:(it's just one of an examples, you need to write code generally) 10 40 Shuch Satp 05:47 89 Kyzy Taldy  07:00...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a...
write this program in c++ using iostream library.( cin>>n; cin>> arr[n] and so on) Write a function that converts an array so that in the first half settled with elements from odd positions, and in the second half - with elements from the even positions.Positions are counted from the first index.The program have to use pointer. example: input: 7 1 2 3 4 5 6 7 output: 1 3 5 7 2 4 6 8
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating...
Plz convert this C++ code into JAVA code thanks #include<iostream> using namespace std; //function for calculating the average sightings from the Total Sightings array float calcAverage(float totalSightings[],int n) {    int i;    float sum=0.0;    for(i=0;i<n;i++)    sum=sum+totalSightings[i];    return sum/n; } int main() {    // n is no. of bird watchers    //flag , flag2 and flag3 are for validating using while loops    int n,i,flag,flag2,flag3;       //ch also helps in validating    char ch;   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT