WAP to add two lengths in feet inch using a structure

Write a program to add two lengths in feet inch using a structure

#include<stdio.h>

struct Distance {
	int feet;
	float inch;
} dist1,dist2,sum;

int main() {
	printf("1st distance\n");

	printf("Enter feet: ");
	scanf("%d",&dist1.feet);

	printf("Enter inch: ");
	scanf("%f",&dist1.inch);

	printf("2nd distance\n");

	printf("Enter feet: ");
	scanf("%d",&dist2.feet);

	printf("Enter inch: ");
	scanf("%f",&dist2.inch);

	//adding feet
	sum.feet = dist1.feet + dist2.feet;

	//adding inches
	sum.inch = dist1.inch + dist2.inch;

	//changing feet if inch is greater than 12
	while(sum.inch>=12){
		++sum.feet;
		sum.inch -= 12;
	}
	printf("Sum of distance = %d\'-%.1f\"",sum.feet,sum.inch);
	return 0;
}

Output

1st distance
Enter feet: 5
Enter inch: 5
2nd distance
Enter feet: 5
Enter inch: 6
Sum of distance = 10′-11.0″

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments