Home C Programming Language / Programs / C Program to Add Two Distances (in inch-feet) System Using Structures
🚀 Programming Example

C Program to Add Two Distances (in inch-feet) System Using Structures

👁 1,172 Views
💻 Practical Program
📘 Step Learning
This program takes two distances (in inch-feet system), adds them and displays the result on the screen.

💻 Program Code

#include <stdio.h>

struct Distance
{
    int feet;
    float inch;
} d1, d2, sumOfDistances;

int main()
{
    printf("Enter information for 1st distance\n");
    printf("Enter feet: ");
    scanf("%d", &d1.feet);
    printf("Enter inch: ");
    scanf("%f", &d1.inch);

    printf("\nEnter information for 2nd distance\n");
    printf("Enter feet: ");
    scanf("%d", &d2.feet);
    printf("Enter inch: ");
    scanf("%f", &d2.inch);


    sumOfDistances.feet = d1.feet+d2.feet;
    sumOfDistances.inch = d1.inch+d2.inch;

    // If inch is greater than 12, changing it to feet.

    if (sumOfDistances.inch>12.0)
    {
        sumOfDistances.inch = sumOfDistances.inch-12.0;
        ++sumOfDistances.feet;
    }

    printf("\nSum of distances = %d\'-%.1f\"",sumOfDistances.feet, sumOfDistances.inch);
    return 0;
}
                        

🖥 Program Output

Enter information for 1st distance
Enter feet: 23
Enter inch: 8.6

Enter information for 2nd distance
Enter feet: 34
Enter inch: 2.4

Sum of distances = 57'-11.0"
                            

📘 Explanation

This program takes two distances (in inch-feet system), adds them and displays the result on the screen.

In this program, a structure Distance is defined. The structure has two members inch (a float) and feet (an integer).

Two variables (d1 and d2) are created which stores two distances (in inch and feet). Then, the sum of two distances is stored in sumOfDistances structure and displayed on the screen.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.