This example has a structure named distance with two integer members feet and inches, we are implementing a user define function named addDistance() that will take two structure objects and print sum (addition) of their elements.
#include <stdio.h>
struct distance{
int feet;
int inch;
};
void addDistance(struct distance d1,struct distance d2){
struct distance d3;
d3.feet= d1.feet + d2.feet;
d3.inch= d1.inch + d2.inch;
d3.feet= d3.feet + d3.inch/12; //1 feet has 12 inches
d3.inch= d3.inch%12;
printf("\nTotal distance- Feet: %d, Inches: %d",d3.feet,d3.inch);
}
int main()
{
struct distance d1,d2;
printf("Enter first distance in feet & inches:");
scanf("%d%d",&d1.feet, &d1.inch);
printf("Enter second distance in feet & inches:");
scanf("%d%d",&d2.feet, &d2.inch);
/*add two distances*/
addDistance(d1,d2);
return 0;
}
Output
Enter first distance in feet & inches: 10 8
Enter second distance in feet & inches: 5 7
Total distance- Feet: 16, Inches: 3
This example has a structure named distance with two integer members feet and inches, we are implementing a user define function named addDistance() that will take two structure objects and print sum (addition) of their elements.
Output
need an explanation for this answer? contact us directly to get an explanation for this answer