×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to add two distances in feet and inches using structure

In this program, we will learn how to add two distances in feet and inches using structure in C programming language?

Program to add two distances in the inch-feet system

This example has a structure named distance with two integer members feet and inches, we are implementing a user define function named addDistance() that will take two structure objects and print sum (addition) of their elements.

#include <stdio.h>

struct distance {
    int feet;
    int inch;
};

void addDistance(struct distance d1, struct distance d2)
{
    struct distance d3;
    d3.feet = d1.feet + d2.feet;
    d3.inch = d1.inch + d2.inch;

    d3.feet = d3.feet + d3.inch / 12; //1 feet has 12 inches
    d3.inch = d3.inch % 12;

    printf("\nTotal distance- Feet: %d, Inches: %d", d3.feet, d3.inch);
}

int main()
{
    struct distance d1, d2;
    
    printf("Enter first distance in feet & inches:");
    scanf("%d%d", &d1.feet, &d1.inch);

    printf("Enter second distance in feet & inches:");
    scanf("%d%d", &d2.feet, &d2.inch);
    
    /*add two distances*/
    addDistance(d1, d2);
    
    return 0;
}

Output

Enter first distance in feet & inches: 10 8
Enter second distance in feet & inches: 5 7
Total distance- Feet: 16, Inches: 3

C Structure & Union Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.