#include <iostream>
using namespace std;
// Function to add two distances and update the first one
void addDistance(int &km1, int &m1, int km2, int m2) {
// Validate meters input
if (m1 < 0 || m1 >= 1000 || m2 < 0 || m2 >= 1000) {
cout << "Error: Meters must be less than 1000!" << endl;
return;
}
// Add kilometers and meters separately
km1 += km2; // Add kilometers
m1 += m2; // Add meters
// If meters exceed 1000, convert them to kilometers
if (m1 >= 1000) {
km1 += m1 / 1000; // Convert excess meters to kilometers
m1 = m1 % 1000; // Keep the remainder as meters
}
// Display the final result
cout << "Total Distance: " << km1 << " Kilometers and " << m1 << " Meters" << endl;
}
int main() {
int km1, m1, km2, m2;
// Input first distance
cout << "Enter the first distance (Kilometers and Meters): ";
cin >> km1 >> m1;
// Input second distance
cout << "Enter the second distance (Kilometers and Meters): ";
cin >> km2 >> m2;
// Call addDistance to compute the total distance
addDistance(km1, m1, km2, m2);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKLy8gRnVuY3Rpb24gdG8gYWRkIHR3byBkaXN0YW5jZXMgYW5kIHVwZGF0ZSB0aGUgZmlyc3Qgb25lCnZvaWQgYWRkRGlzdGFuY2UoaW50ICZrbTEsIGludCAmbTEsIGludCBrbTIsIGludCBtMikgewogICAgLy8gVmFsaWRhdGUgbWV0ZXJzIGlucHV0CiAgICBpZiAobTEgPCAwIHx8IG0xID49IDEwMDAgfHwgbTIgPCAwIHx8IG0yID49IDEwMDApIHsKICAgICAgICBjb3V0IDw8ICJFcnJvcjogTWV0ZXJzIG11c3QgYmUgbGVzcyB0aGFuIDEwMDAhIiA8PCBlbmRsOwogICAgICAgIHJldHVybjsKICAgIH0KCiAgICAvLyBBZGQga2lsb21ldGVycyBhbmQgbWV0ZXJzIHNlcGFyYXRlbHkKICAgIGttMSArPSBrbTI7ICAvLyBBZGQga2lsb21ldGVycwogICAgbTEgKz0gbTI7ICAgIC8vIEFkZCBtZXRlcnMKCiAgICAvLyBJZiBtZXRlcnMgZX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