✓ Link copied to clipboard
C 📅 July 11, 2026 ⏱ 5 min read

C Program to Add Two Distances in Inch-Feet Using Structures

C Program to Add Two Distances in Inch-Feet Using Structures

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:

  • struct keyword 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.

add_distances.cC▶ Run
#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.

▶ Open Compiler

Output of the Add Two Distances Program

This is what you'll see when you run the code with sample inputs.

Output
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
Output of C Program to Add Two Distances (in inch-feet system) using Structures
Output of C Program to Add Two Distances (in inch-feet system) using Structures

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:

  1. Define a structure to hold feet and inches.
  2. Read the first distance's feet and inches from the user.
  3. Read the second distance's details.
  4. Add the feet and inches separately.
  5. If the inches total 12 or more, convert them to feet.
  6. 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])
Flowchart: the algorithm step by step

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 for printf and scanf functions.
  • 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 three Distance structures to hold the input and result.
  • scanf: Gets the distances from the user.
  • Addition: result.feet = d1.feet + d2.feet and result.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 printf to display the final result.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What must you do if the total inches exceed 12 when adding distances?

🎉Correct! Converting excess inches to feet maintains the correct distance format.
🤔Not quite. Remember, 12 inches make 1 foot.

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.

Structures help group related data, like feet and inches. It makes the program cleaner and modular.
If total inches hit 12 or more, it's converted to extra feet and subtracted from inches.
Nope, it assumes distances are positive. Dealing with negatives needs extra checks.
It checks if inches go over 12. That way, it converts them into feet to keep things accurate.
Inches are stored as floats. This precision keeps your addition and conversion spot on.

If you found this program helpful, check out these related programs:

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.