In this program, you'll learn to declare structure "distance" having member variables are km and m. Accept the two distances and you will get the total distance.
#include<stdio.h>
struct distance
{
int km, m;
};
int main()
{
struct distance d1, d2, d3;
printf("Enter first distance KM and M: ");
scanf("%d%d", &d1.km, &d1.m);
printf("Enter second distance KM and M: ");
scanf("%d%d", &d2.km, &d2.m);
d3.m = d1.m + d2.m;
d3.km = d1.km + d2.km;
if(d3.m >=1000)
{
d3.m = d3.m - 1000;
d3.km++;
}
printf("\nTotal distance is: %d km, %d m", d3.km, d3.m);
return 0;
}
Enter first distance KM and M: 5 750
Enter second distance KM and M: 12 500
Addition of distances is: 18 km, 250 m