Calculate a+b and output the sum in standard format – that is, the digits must be separated into groups of three by commas (unless there are less than four digits).
Input Specification:
Each input file contains one test case. Each case contains a pair of integers a and b where − 1 0 6 −10^6 −106≤a,b≤ 1 0 6 10^6 106. The numbers are separated by a space.
Output Specification:
For each test case, you should output the sum of a and b in one line. The sum must be written in the standard format.
Sample Input:
-1000000 9
Sample Output:
-999,991
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int a, b;
cin >> a >> b;
int c = a + b;
string ans = "";
bool flag = false;
if (c < 0) flag = true;
c = abs(c);
int cnt = 0;
if (c == 0) {
cout << 0 << "\n";
return 0;
}
while (c){
if (cnt == 3) {
cnt = 0;
ans = "," + ans;
}
int x = c % 10;
c /= 10;
ans = (char)(x + '0')+ ans;
cnt++;
}
if (flag) ans = "-" + ans;
cout << ans << '\n';
return 0;
}