Signup/Sign In

Add Two Distances System Using Structures in C language

A lot of measuring methods involves feet and inches, 12 inches is equal to 1 foot, in the majority of the measuring systems. In this program, A structure is defined as Distance, The Structure "Distance" has two members:

  • Feet have whole values so, It is taken as an Integer type,
  • Inches have Decimal values so, It is taken as Float type,

Here, Two variables are declared x1 and x2, In Struct distance, these two variables are created, These two variables can store distances in feet and inches, After the values are stored, two distances are computed and the result is stored in another variable.

The Result Variable is displayed on the print screen.

Algorithm To Add Two Distance System Using Structure:

  • Declare a Structure "Distance", Which has two variables containing feet and inches.
  • Get the input from the user which has both distances feet and inches, and store the values in respective variables.
  • Add the values inches and feet, and put the value in another variable.
  • Now, Change the turn the inches values into feet, For every inch, greater than 12 in the sum, Decrement the value by 12.
  • Increment the value of feet by 1, After the execution of the process print the value in Print Screen.

C Program To Add Two Distance System Using Structure:

#include <stdio.h>

struct Distance
  int feet;
  float inch;
} firstDistance, secondDistance, sum;

int main()
  printf("Enter feet and inches for the first distance: \n");
  scanf("%d %f", &firstDistance.feet, &firstDistance.inch);

  printf("Enter feet and inches for the second distance: \n");
  scanf("%d %f", &secondDistance.feet, &secondDistance.inch);

  sum.feet = firstDistance.feet + secondDistance.feet;
  sum.inch = firstDistance.inch + secondDistance.inch;

  while (sum.inch >= 12)
    sum.inch = sum.inch - 12;

  printf("The Sum is %d feet, %.1f inches\n", sum.feet, sum.inch);
  return 0;

  • After the values are read from the user, Add the Feet and Inches values of both variables and store the resultant value.
  • Here, the while loop transforms inches to feet, and decrementation of the values takes place ( decrements the inches value to less than 12 ) Adds to inches.


Feet inch