C Program to Add Two Distances (in inch-feet) System Using Structures
This program takes two distances (in an inch-feet system), adds them and displays the result on the
To understand this example, you should have the knowledge of following C programming topics:
Example: Program to add two distances in an inch-feet system
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} d1, d2, sumOfDistances;
int main()
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
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("\nSum of distances = %d\'-%.1f\"",sumOfDistances.feet, sumOfDistances.inch);
return 0;
}
Output
Enter information for 1st distance Enter feet: 23 Enter inch: 8.6 Enter information for 2nd distance Enter feet: 34 Enter inch: 2.4 Sum of distances = 57'-11.0"
In this program, a structure Distance
is defined. The structure has two members inch
(a float) and feet
(an integer).
Two variables (d1 and d2) are created which stores two distances (in inch and feet). Then, the sum of two distances is stored in the sumOfDistances structure and displayed on the screen.
C program to add two distances (in inch-feet) system using structures:
#include <stdio.h>
struct Distance {
int feet;
float inch;
};
void add(struct Distance d1, struct Distance d2, struct Distance *result) {
result->inch = d1.inch + d2.inch;
result->feet = 0;
if (result->inch >= 12.0) {
result->inch -= 12.0;
result->feet++;
}
result->feet += d1.feet + d2.feet;
}
int main() {
struct Distance d1, d2, result;
// Input for d1
printf("Enter feet and inch for distance 1:\n");
scanf("%d %f", &d1.feet, &d1.inch);
// Input for d2
printf("Enter feet and inch for distance 2:\n");
scanf("%d %f", &d2.feet, &d2.inch);
// Add the two distances
add(d1, d2, &result);
// Output
printf("Sum = %d\'-%.1f\"", result.feet, result.inch);
return 0;
}
Explanation:
#include <stdio.h>
includes the standard input-output header file in our program, which provides input and output operations functionality.struct Distance {...};
defines a structure namedDistance
which has two members:feet
andinch
.void add(struct Distance d1, struct Distance d2, struct Distance *result) {...}
defines a function namedadd()
which takes two distances as input and returns their sum as a distance. The result is stored in the third parameterresult
, which is a pointer to aDistance
structure.int main()
is the entry point of our program.struct Distance d1, d2, result;
declares three variablesd1
,d2
, andresult
of typeDistance
.- The next four statements use
printf()
andscanf()
to input the feet and inch values of the two distances. add(d1, d2, &result);
calls theadd()
function and passes it the two distances as well as a pointer to theresult
structure.printf("Sum = %d\'-%.1f\"", result.feet, result.inch);
prints the sum of the two distances usingprintf()
. The%d
format specifier is used to print thefeet
value as an integer, and%.1f
is used to print theinch
value with one decimal place. The backslash (\
) before the single quote ('
) and double quote ("
) characters is an escape sequence to print those characters.
When we compile and run this program, it will prompt the user to input the feet and inch values of two distances and then display their sum in the same format.