C++ Program to Add Two Distances (in inch-feet) System Using Structures

#include <iostream>
using namespace std;
// Structure storing distance in inch and feet
struct Distance{
    int feet;
    float inch;
};
int main() {
 Distance d1, d2, d3;
    cout << "Enter first distance as [feet inch]\n";
    cin >> d1.feet >> d1.inch;
    cout << "Enter second distance as [feet inch]\n";
    cin >> d2.feet >> d2.inch;
    // Adding d1 and d2 and storing the sum in d3
    d3.feet = d1.feet + d2.feet;
    d3.inch = d1.inch + d2.inch;
    // NOTE : 12 inch = 1 feet
 // If feet > 12 then feet = feet%12 and inch++
    if(d3.inch > 12){
        d3.feet++;
        d3.inch = d3.inch - 12;
    }
    cout << "Total distance = " << d3.feet << " feet, " << d3.inch <<" inches";
    return 0;
}

OUTPUT:

Enter first distance as [feet inch]
5 7
Enter second distance as [feet inch]
3 8
Total distance = 9 feet, 3 inches

Leave a Comment