C program to Add two Distances (in inch-feet system) using Structures

In this C Programming example, we will implement the program to add two distances (in an inch-feet system) using structures and print the output on the console.

1. How do you add two distances in inch feet?

In the following program, we will add two distances in the inch-feet system using a user-defined structure.

We constructed a custom structure called Distance that contains two data member variables feet and inch.

Structure-

typedef struct {
     int feet;
     int inch;
 }Distance;
Example

Input:
Feet and Inch of First Distance: 2 5
Feet and Inch of Second Distance: 5 4

Output: 
Sum is 7' 9''

Helpful topics to understand this program better are-


2. C Program to add two distances (in an inch-feet system) using structures

Let’s discuss the execution(kind of pseudocode) for the program to add two distances (in an inch-feet system) using structures in C.

  1. Initially, the user is prompted to enter the feet and inch of the first and second distance.
  2. A Structure Distance holding two data members (inch and feet) is declared to store the distance in the inch-feet system.
  3. The two structure variables d1 and d2 are declared to store the distance entered by the use. The Sum variable stores the sum of the distances.
  4. Next, we calculate sum inch and sum feet and use an if-statement to evaluate the condition if (Sum.inch >= 12.0){}.
  5. After the calculations are completed the results are printed on the console.

Let us now implement the above execution of the program to add two distances (in an inch-feet system) using structures in C.

#include <stdio.h>
typedef struct {
  int feet;
  int inch;
} Distance;

int main() {
  Distance d1, d2, Sum;

  printf("Enter the feet and inch of first distance: ");
  scanf("%d%d", &d1.feet, &d1.inch);
  printf("Enter the feet and inch of second distance: ");
  scanf("%d%d", &d2.feet, &d2.inch);

  Sum.inch = d1.inch + d2.inch;
  Sum.feet = d1.feet + d2.feet;

  if (Sum.inch >= 12.0) {
    Sum.inch = Sum.inch - 12.0;
    ++Sum.feet;
  }
  printf("The Sum is %d' %d''", Sum.feet, Sum.inch);
  return 0;
}
Output
Enter the feet and inch of first distance: 5
12
Enter the feet and inch of second distance: 5
4
The Sum is 11' 4''

3. Conclusion

In this C Programming example, we have discussed how to add two distances (in an inch-feet system) using structures in C.


Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index