C Program to Add Two Distances (inch-feet) System by Passing Structure to a Function

#include<stdio.h>
#include<conio.h>

void addition(struct dist,struct dist,struct dist *);

struct dist
{
  int ft; //feet=plural of foot
  int in; //1 foot=12 inch
};

void main()
{
  struct dist d1,d2,d3;
  printf("Enter feet and inch for 1st system: ");
  scanf("%d%d",&d1.ft,&d1.in);
  printf("\nEnter feet and inch for 2nd system: ");
  scanf("%d%d",&d2.ft,&d2.in);
  addition(d1,d2,&d3);
  printf("\nAddition of two systems is %d ft %d in",d3.ft,d3.in);
  getch();
}

void addition(struct dist dd1,struct dist dd2,struct dist *dd3)
{
  (*dd3).ft=dd1.ft+dd2.ft;
  (*dd3).in=dd1.in+dd2.in;
  if((*dd3).in>=12)
  {
    (*dd3).ft++;
    (*dd3).in-=12;
  }
}


OUTPUT:

No comments:

Post a Comment