Starting Vocational Training From 1-May-2024 Get Detail


With the help of structure add distance between two points in feet and inches. 


#include<stdio.h>

struct Distance {

int feet;

float inch;

} d1, d2, sumOfDistances;


int main() {

printf("Enter information for 1st distance ");

printf("Enter feet: ");

scanf("%d", &d1.feet);

printf("Enter inch: ");

scanf("%f", &d1.inch);

printf(" Enter information for 2nd distance ");

printf("Enter feet: ");

scanf("%d", &d2.feet);

printf("Enter inch: ");

scanf("%f", &d2.inch);


sumOfDistances.feet=d1.feet+d2.feet;

sumOfDistances.inch=d1.inch+d2.inch;


// If inch is greater than 12, changing it to feet.

if (sumOfDistances.inch>12.0) {

  sumOfDistances.inch = sumOfDistances.inch-12.0;

   ++sumOfDistances.feet;

}

printf(" Sum of distances = %d-%.1f"", sumOfDistances.feet, sumOfDistances.inch);

return 0;

}