Write a C program to add two distances using structures:
This program will show you how to add two distance values using structures. The distance values are represented in feet and inches. The program will take the values as input from the user and find out the sum.
To solve this program with C programming Structure
, we will create one custom Structure
that can hold the values of the feet and inches values in two different data members. We will create two variables of that Structure
and user-provided values will be assigned to the data members of these variables. We will also create one more Structure
variable to hold the sum. The calculated result will be assigned to the data members of the result variable.
The program will use the following algorithm:
Algorithm to add two distances using structures in C:
- Define one
Structure
with two data members to hold the feet and inch values. - Create three variables of the
Structure
. One of these variables is used to store the result and the other two are used to store the user-given values. - Ask the user to enter the feet and inch values for both distances. Read and assign these values to the data members of the
Structure
s. - Add the feet and inch values of both distance variables. Assign these values to the data members of the result variable.
- We will add the feet and inches values separately. 1 foot is equal to 12 inches. For each inch > 12 in the sum, we need to decrement it by 12 and increment the value of feet by 1. It will convert the inch value to feet.
- Once the conversion is completed, print the value of the values stored in the sum variable.
Method 1: C Program to add two distances using structures:
Below is the complete C program:
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} firstDistance, secondDistance, sum;
int main()
{
printf("Enter feet and inch for the first distance with a space : \n");
scanf("%d %f", &firstDistance.feet, &firstDistance.inch);
printf("Enter feet and inch for the second distance with a space : \n");
scanf("%d %f", &secondDistance.feet, &secondDistance.inch);
sum.feet = firstDistance.feet + secondDistance.feet;
sum.inch = firstDistance.inch + secondDistance.inch;
while (sum.inch >= 12)
{
sum.inch = sum.inch - 12;
sum.feet++;
}
printf("Sum is %d feet, %.1f inches\n", sum.feet, sum.inch);
return 0;
}
Explanation:
- The
Distance
is a structure to hold a distance value. It can hold an integer variable for the feet and one floating point variable for the inch value. - The
firstDistance
and thesecondDistance
variables are created to store the first and the second distance values entered by the user. Thesum
variable is initialized to hold the final sum. - It asks the user to enter the values of the distances one by one. For each distance, it asks the user to enter the feet and inch values. It assigns these values to the member variables of the
firstDistance
andsecondDistance
variables. - Once the values are assigned, it adds the feet and inch values of the structure member variables and assigns these values to the
sum
variable members. - The
while
loop converts the value of the inch value to feet. - The last line is printing the calculated sum of the distances.
Sample output:
If you run the above program, it will print outputs as below:
Enter feet and inch for the first distance with a space :
12 30
Enter feet and inch for the second distance with a space :
20 40
Sum is 37 feet, 10.0 inches
Download this program on Github
Method 2: How to add two distances with Structure and division operation:
In the above example, we have used a while
loop to convert the final inch value to feet. We can also use the division and modulo operations to do the conversion. For example, if the final result is 22 feet, 82 inches, we need to follow the following two steps for the feet-inches conversion:
- Divide the inch value by 12 and add it to the feet value. This will be the new feet value. For 82, it will be
82/12 = 6
i.e. the final feet value is22 + 6 = 28
. - Get the remainder by dividing the inch value by 12 and assign it to the inch variable. For 82, it will be
82 % 12 = 10
i.e. the final inch value is10
.
The following program uses the above steps to add the distances:
#include <stdio.h>
#include <math.h>
struct Distance
{
float feet;
float inch;
} firstDistance, secondDistance, sum;
int main()
{
printf("Enter feet and inch for the first distance with a space : \n");
scanf("%f %f", &firstDistance.feet, &firstDistance.inch);
printf("Enter feet and inch for the second distance with a space : \n");
scanf("%f %f", &secondDistance.feet, &secondDistance.inch);
sum.feet = firstDistance.feet + secondDistance.feet;
sum.inch = firstDistance.inch + secondDistance.inch;
if (sum.inch >= 12)
{
sum.feet += (int)(sum.inch / 12);
sum.inch = fmod(sum.inch, 12);
}
printf("Sum is %.1f feet, %.1f inches\n", sum.feet, sum.inch);
return 0;
}
Here, we are casting the division result to an integer (int)(sum.inch / 12)
and the fmod
function is used to find the remainder. If you run this program, it will give similar results.
Enter feet and inch for the second distance with a space :1 11
Enter feet and inch for the second distance with a space :
1 1
Sum is 3 feet, 0.0 inches
Download this program on Github
You might also like:
- Escape sequence in C explanation with example
- C logical AND, && explanation with examples
- C program to convert a string to uppercase or capitalize all characters
- C program to check if a character is a vowel or consonant
- C program to print squares and cubes of all numbers from 1 to n
- How to declare a variable dynamically in C
- Difference between %d and %i format specifiers in C