assignment-s-24.c (935B)
1 /* Write a program to calculate the difference between two time periods using structures. */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 struct time_dif 7 { 8 int hour; 9 int min; 10 int sec; 11 }; 12 13 int main() 14 { 15 struct time_dif tpA, tpB; 16 long int tfA, tfB, tdif; 17 printf("Enter the start time in this 24 hours clock format (HH MM SS): "); 18 scanf("%d %d %d", &tpA.hour, &tpA.min, &tpA.sec); 19 printf("Enter the end time in this 24 hours clock format (HH MM SS): "); 20 scanf("%d %d %d", &tpB.hour, &tpB.min, &tpB.sec); 21 22 tfA = tpA.hour * 3600 + tpA.min * 60 + tpA.sec; 23 tfB = tpB.hour * 3600 + tpB.min * 60 + tpB.sec; 24 tdif = tfB - tfA; 25 if (tdif < 0) 26 { 27 printf("\nWrong information, End time should be later than Start time."); 28 return 1; 29 } 30 printf("\nTime difference: %ld Hour(s) %ld Minutes(s) %ld Second(s).", tdif / 3600, (tdif % 3600) / 60, (tdif % 3600) % 60); 31 32 return 0; 33 }