C Program to Add Two Distances (in inch-feet) system using Structures
ADVERTISEMENTS
C Program to Add Two Distances (in inch-feet) system using Structures.
In this article, you will learn how to add the values of two distances which contain feet
& inches
values using the C structure.
Example
Enter the 1st distance in Feet: 12
Enter the 1st distance in Inches: 6.8
Enter the 2nd distance in Feet: 15
Enter the 2nd distance in Inches: 11.9
Final distances: 28'-6.7"
You should know about the following topics in C programming to understand this program:
- C Structure
- C While Loop
Source
// C Program to Add Two Distances (in inch-feet) system using Structures
#include <stdio.h>
// Make structure of the distance
struct {
int feet;
float inches;
} x, y, results;
// It's the main function of the program
int main() {
// Step-1 INPUT of the first distance
printf("Enter the 1st distance in Feet: ");
scanf("%d", &x.feet);
printf("Enter the 1st distance in Inches: ");
scanf("%f", &x.inches);
// Step-2 INPUT of the second distance
printf("\nEnter the 2nd distance in Feet: ");
scanf("%d", &y.feet);
printf("Enter the 2nd distance in Inches: ");
scanf("%f", &y.inches);
// Step-3 Calculate the values of these distances to make sum
results.feet = x.feet + y.feet;
results.inches = x.inches + y.inches;
// Step-4 It will check the value of the results.inches
// If its value is greater or equal to 12 then It will convert and add this value in results.feet
while (results.inches >= 12.0) {
results.inches = results.inches - 12.0;
results.feet++;
}
// Step-5 Final output of the program
printf("\nFinal distances: %d\'-%.1f\"\n", results.feet, results.inches);
return 0;
}
Output
Enter the 1st distance in Feet: 30
Enter the 1st distance in Inches: 6.6
Enter the 2nd distance in Feet: 85
Enter the 2nd distance in Inches: 1
Final distances: 115'-7.6"