ADVERTISEMENT
ADVERTISEMENT

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?

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.