In this program, we will learn how to write a C Program to add two distances using structure.
C Program to add two user input distances using structure
The explanation of the program is provided at the end of the following code. The brief description of important statements and function is provided in the program itself using comments.
#include <stdio.h>
// Structure to store distance in feet and inches
struct Distance {
int feet; //to hold feet in distance
float inches; //to hold inches in distance
};
// Function to add two distances
struct Distance addDistances(struct Distance d1, struct Distance d2) {
struct Distance result;
// Add feet and inches separately
result.feet = d1.feet + d2.feet;
result.inches = d1.inches + d2.inches;
// Since 1 feet contains 12 inches, if inches is greater
// than or equal to 12, convert extra inches to feet
if (result.inches >= 12.0) {
result.feet += (int)(result.inches / 12);
result.inches = fmod(result.inches, 12.0);
}
return result;
}
int main() {
struct Distance d1, d2, result;
// Prompt user to enter the first distance
printf("Enter feet and inches for the first distance: ");
scanf("%d %f", &d1.feet, &d1.inches);
// Prompt user to enter the second distance
printf("Enter feet and inches for the second distance: ");
scanf("%d %f", &d2.feet, &d2.inches);
// Call the function to Add the entered distances
result = addDistances(d1, d2);
// Print output
printf("Sum of distances: %d feet %.2f inches\n", result.feet, result.inches);
return 0;
}
Output: Assume user enters the first distance as 4 7.5 where 4 is feet and 7.5 is inches. The second distance entered by user is 6 6.5.
Enter feet and inches for the first distance: 4 7.5
Enter feet and inches for the second distance: 6 6.5
Sum of distances: 11 feet 2.00 inches
How this program works?
Step 1: Define the Structure
We defined a structure Distance
with two fields, feet and inches.
#include <stdio.h>
#include <math.h> // For fmod function
struct Distance {
int feet;
float inches;
};
Step 2: Function to Add Two Distances
We defined a function addDistances()
that takes two Distance
structures as parameters, adds them, and returns the result.
result.feet = d1.feet + d2.feet;
andresult.inches = d1.inches + d2.inches;
add the respective feet and inches.if (result.inches >= 12.0) { ... }
checks if inches are 12 or more.result.feet += (int)(result.inches / 12);
converts excess inches to feet.result.inches = fmod(result.inches, 12.0);
uses thefmod
function to keep only the fractional part less than 12. (Note: Thefmod
function in C is used to compute the remainder of the division of two floating-point numbers. It is part of the<math.h>
library)
Step 3: Main Function to Get Input and Display Output
In the main
function, we prompt user to enter two distances, call the addDistances()
function to add them and then print the result
struct Distance d1, d2, result;
declares threeDistance
variables.d1
for first distance,d2
for second distance andresult
to contain addition ofd1
andd2
.printf
andscanf
statements are used to take input from user and store the entered values in structuresd1
andd2
.result = addDistances(d1, d2);
calls the function to add distances.printf("Sum of distances: %d feet %.2f inches\n", result.feet, result.inches);
prints the result.
Leave a Reply