# Sum & Difference of Start & Stop Time Using Structure & User Defined Function

Question: Create a structure TIME containing hour,minutes and seconds as its member. Write a program that uses this structure to input start time and stop time in main(). Pass this structure to a function that calculates the sum and difference of start time and stop time. Display the sum and difference from main().

``````
#include<stdio.h>

/* Creating Structure */
typedef struct
{
int hours;
int minutes;
int seconds;
}TIME;

/* Function Prototypes */
TIME subtract(TIME, TIME);

int main()
{
TIME start, stop, sum, diff;
clrscr();
printf("Enter hours, minutes and seconds of start time: ");
scanf("%d%d%d", &start.hours, &start.minutes, &start.seconds);
printf("Enter hours, minutes and seconds of stop time: ");
scanf("%d%d%d", &stop.hours, &stop.minutes, &stop.seconds);

diff = subtract(start, stop);

printf("Sum: %d:%d:%d\n", sum.hours, sum.minutes, sum.seconds);
printf("Difference: %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);

getch();
return 0;
}

{
TIME sum;
sum.seconds = x.seconds + y.seconds;
sum.minutes = x.minutes + y.minutes;
sum.hours = x.hours + y.hours;
if(sum.seconds >= 60)
{
sum.minutes++;
sum.seconds -= 60;
}
if(sum.minutes >= 60)
{
sum.hours ++;
sum.minutes -= 60;
}
return sum;
}

TIME subtract(TIME x, TIME y)
{
TIME diff;
if(x.seconds > y.seconds)
{
y.seconds += 60;
--y.minutes;
}
if(x.minutes > y.minutes)
{
y.minutes += 60;
--y.hours;
}

diff.seconds = y.seconds - x.seconds;
diff.minutes = y.minutes - x.minutes;
diff.hours = y.hours - x.hours;
return diff;
}
``````

Output

```Enter hours, minutes and seconds of start time: 2 40 50
Enter hours, minutes and seconds of stop time: 4 30 40
Sum: 7:11:30
Difference: 1:49:50
```