The input for this program consists of numbers n, a0, a1, ..., a{n-1}, b0, b1, b{n-1}. In this, ai and bi are supposed to be the ith least significant digits of numbers A, B. You are supposed to print the digits of the sum of A, B, again from least significant to the most significant - one per line. Note that the sum can have n+1 digits, and so you should print n+1 digits, with the most significant digit being possibly 0.You may assume that the size of an array can be a value that is known only during execution. For example, it is OK to write
"int n; cin >>n; int A[n];"
1
Expert's answer
2020-03-18T13:10:23-0400
#include<iostream>usingnamespace std;
intmain(){
int n;
cout << "\nInput amount of digits in a number: ";
cin >> n;
int* A=newint[n];
int* B = newint[n];
cout << "\nInput number A by digits (" << n << "digits) :";
for (int i = 0; i < n; i++)
cin >> A[i]; //input number A
cout << "\nInput number B by digits (" << n << "digits) :";
for (int i = 0; i < n; i++)
cin >> B[i]; //input number B
cout << "\nSum of numbers A and B: ";
for (int i = n - 1; i > 0; i--)
{ //sum of first n-1 digits
cout << endl << (A[i] + B[i]) % 10;
A[i - 1] += (A[i] + B[i])/10;
}
//sum of two the most significant numbers
cout << endl << (A[0] + B[0]) % 10<<endl<< (A[0] + B[0]) / 10;
//memory clearingdelete[]A;
delete[]B;
}
"assignmentexpert.com" is professional group of people in Math subjects! They did assignments in very high level of mathematical modelling in the best quality. Thanks a lot
Comments