Answer to Question #105791 in C++ for ravneet

Question #105791
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>
using namespace std;
int main()
{
	int n;
	cout << "\nInput amount of digits in a number: ";
	cin >> n;
	int* A=new int[n];
	int* B = new int[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 clearing
	delete[]A;
	delete[]B;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS