File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+
2+ #include < iostream>
3+ #include < vector>
4+ using namespace std ;
5+
6+ int main () {
7+ const int n = 3 ;
8+ vector<int > l1 (n), l2 (n), result (n);
9+ int carry = 0 ;
10+
11+ cout << " list 1:" << endl;
12+ for (int i = 0 ; i < n; i++) {
13+ cin >> l1[i];
14+ if (l1[i] < 0 || l1[i] > 9 ) {
15+ cout << " Enter a number from 0 to 9" << endl;
16+ return 1 ;
17+ }
18+ }
19+
20+ cout << " list 2:" << endl;
21+ for (int i = 0 ; i < n; i++) {
22+ cin >> l2[i];
23+ if (l2[i] < 0 || l2[i] > 9 ) {
24+ cout << " Enter a number from 0 to 9" << endl;
25+ return 1 ;
26+ }
27+ }
28+
29+ for (int i = 0 ; i < n; i++) {
30+ int sum = l1[i] + l2[i] + carry;
31+ result[i] = sum % 10 ;
32+ carry = sum / 10 ;
33+ }
34+
35+
36+ cout << " [" ;
37+ for (int i = 0 ; i < n; i++) {
38+ cout << result[i];
39+ if (i < n - 1 ) cout << " ," ;
40+ }
41+ if (carry > 0 ) cout << " ," << carry;
42+ cout << " ]" << endl;
43+
44+ return 0 ;
45+ }
You can’t perform that action at this time.
0 commit comments