1. In C++, the largest integer is 2,147,483,647. To manipulate numberslarger tha
ID: 3618072 • Letter: 1
Question
1. In C++, the largest integer is 2,147,483,647. To manipulate numberslarger than this we can store each digit of the number in an array.Write a program that inputs 2 positive integers of at most 20digits and output the sum of the numbers. If the sum has more than20 digits provide a corresponding. Your program must at least havea function to read and store a number into an array and anotherfunction to output the sum of the numbers. (Hint: read the numbersas strings and store the digits of the number in reverseorder.)
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
char num1[20];
char num2[20];
int num3[21];
int num4[20];
int num5[20];
int i,d=0;
cout<<"Please enter the first number:";
for (i=0;i<20;i++)
{
cin>>num1[19-i];
}
cout<<"Please enter the second number:";
for (i=0;i<20;i++)
{
cin>>num2[19-i];
}
for (i=19;i>=0;i--)
{
char ch1,ch2;
num4[i]=num1[i]-48;
num5[i]=num2[i]-48;
}
for (i=0;i<20;i++)
{
num3[i]=num4[i]+num5[i]+d;
if(num3[i]>9)
{
num3[i]=num3[i]%10;
d=1;
}
else
d=0;
}
num3[20]=d;
cout<<"The sum of those two numbersis: ";
for (i=20;i>=0;i--)
{
cout<<num3[i];
}
cout<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.