The Program in C to Create a Structure Distance,read two distances in-feet and inches and print there Additions is given below:
#include <stdio.h>
struct Distance {
int feet;
int inches;
};
int main() {
struct Distance d1, d2, d3;
printf("Enter first distance (in feet and inches): ");
scanf("%d %d", &d1.feet, &d1.inches);
printf("Enter second distance (in feet and inches): ");
scanf("%d %d", &d2.feet, &d2.inches);
d3.inches = d1.inches + d2.inches;
d3.feet = d1.feet + d2.feet + (d3.inches / 12);
d3.inches = d3.inches % 12;
printf("Addition of two distances: %d feet %d inches", d3.feet, d3.inches);
return 0;
}
Output:
Enter first distance (in feet and inches): 5
9
Enter second distance (in feet and inches): 7
9
Addition of two distances: 13 feet 6 inches
Pro-Tips💡
This program uses the scanf
function to read input from the user and store it in the members of the Distance
structures.
The addition of the two distances is calculated by adding the feet and inches separately, then adjusting for any inches that exceed 12.
The result is then printed using the printf
function.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.