Adding distances in the inch-feet system can be super useful when you're working with measurements. This program shows how to add two distances using structures in C. If you've got two distances like 5 feet 9 inches and 3 feet 11 inches, this code will help you find the total.
We'll use structures to track feet and inches separately. It really shows how organizing data in C can be elegant.
Prerequisites for the C Program to Add Distances
Here's what you'll need before writing this program:
structkeyword for defining structures- Basic understanding of addition operations
- Conditional statements such as
if - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Add Two Distances
Here's the complete source code for adding two distances in inch-feet format using structures.
#include <stdio.h>
struct Distance {
int feet;
float inches;
};
int main() {
struct Distance d1, d2, result;
// Input the first distance
printf("Enter feet for first distance: ");
scanf("%d", &d1.feet);
printf("Enter inches for first distance: ");
scanf("%f", &d1.inches);
// Input the second distance
printf("Enter feet for second distance: ");
scanf("%d", &d2.feet);
printf("Enter inches for second distance: ");
scanf("%f", &d2.inches);
// Add feet and inches separately
result.feet = d1.feet + d2.feet;
result.inches = d1.inches + d2.inches;
// Convert inches to feet if >= 12
if (result.inches >= 12.0) {
result.inches -= 12.0;
result.feet += 1;
}
// Output the result
printf("Total Distance = %d feet %.1f inches\n", result.feet, result.inches);
return 0;
}
Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Add Two Distances Program
This is what you'll see when you run the code with sample inputs.
Enter feet for first distance: 5
Enter inches for first distance: 9
Enter feet for second distance: 3
Enter inches for second distance: 11
Total Distance = 9 feet 8 inches
Algorithm to Add Distances in C
Adding distances involves summing feet and inches and converting excess inches into feet. Here's the logic in steps:
- Define a structure to hold feet and inches.
- Read the first distance's feet and inches from the user.
- Read the second distance's details.
- Add the feet and inches separately.
- If the inches total 12 or more, convert them to feet.
- Output the final distance.
flowchart TD
A([Start]) --> B["Define structure Distance"]
B --> C["Declare d1, d2, result"]
C --> D[/Read d1.feet and d1.inches/]
D --> E[/Read d2.feet and d2.inches/]
E --> F[Add feet and inches separately]
F --> G{"result.inches >= 12?"}
G -->|Yes| H["result.inches -= 12"]
H --> I["result.feet += 1"]
G -->|No| J[Output result]
I --> J
J --> K([End])How the C Program to Add Distances Works
Let's walk through how each part of the code works to add the distances.
#include <stdio.h>: This includes the standard input-output library required forprintfandscanffunctions.struct Distance: Defines a data type storing feet and inches.main(): The main function where the program execution begins.struct Distance d1, d2, result: Declare threeDistancestructures to hold the input and result.scanf: Gets the distances from the user.- Addition:
result.feet = d1.feet + d2.feetandresult.inches = d1.inches + d2.inches. This adds feet and inches separately. if (result.inches >= 12.0): Checks if inches exceed 12.result.inches -= 12.0: Converts 12 inches to an additional foot.- Output: Uses
printfto display the final result.
Quick Quiz — test your understanding
Tap an option to check your answerWhat must you do if the total inches exceed 12 when adding distances?
Time and Space Complexity of Adding Distances in C
Adding distances in the inch-feet system has simple time and space requirements.
Time complexity: O(1). The program performs a fixed number of operations regardless of the input, such as reading input with scanf and checking a single condition.
Space complexity: O(1). It uses only a few variables: d1, d2, and result, each having a fixed size, regardless of the input size.
Common mistake:Not converting inches over 12 to feet gives wrong results.
Key takeaway:Structures give you a neat way to handle complex data like distances, making operations simpler.
FAQs About Adding Two Distances in C
Here are some common questions students ask about this distance-adding program.
Related Programs
If you found this program helpful, check out these related programs:
- C Program to Store Information of Students Using Structure
- C Program to Swap Two Numbers Using Pointers
- C Program to Store Data in Structures Dynamically
- C Program to Add Two Complex Numbers by Passing Structure
- C Program to Find Largest Number Using Dynamic Memory Allocation
Conclusion
Structures in C make handling complex data like distances easier. This program shows how they simplify adding distances in the inch-feet system. Try tweaking it to convert feet into meters. You'll learn even more!
Practice more with our C programming quizzes or run this code in our online compiler.