C Program to Add Two Distance in Feet Inch System Using User Defined Function

This C program add two distances in feet-inch system using user defined function.

C Source Code: Add Distance in Feet Inch System Using Function


#include<stdio.h>

/* Declaring structure */
struct distance
{
    int feet;
    int inch;
};

/* Function prototype */
struct distance add(struct distance x, struct distance y);

/* Main function */
int main()
{
    /* Declaring structure variables */
    struct distance d1, d2, sum;
    
    /* Inputs */
    printf("Enter feet and inch of first distance: ");
    scanf("%d%d", &d1.feet, &d1.inch);
    printf("Enter feet and inch of second distance: ");
    scanf("%d%d", &d2.feet, &d2.inch);
    
    /* Function call */
    sum = add(d1,d2);

    printf("Sum is %d' %d''", sum.feet, sum.inch);
    return 0;
}

struct distance add(struct distance x, struct distance y)
{
    struct distance s;
    s.inch = x.inch + y.inch;
    s.feet = x.feet + y.feet;

    if(s.inch>=12)
    {
        s.feet += s.inch/12;
        s.inch %= 12;
    }
    return s;
}

Output

The output of the above program is:

Enter feet and inch of first distance: 9 11
Enter feet and inch of second distance: 8 8
Sum is 18' 7''