Write a C Program to Calculate Difference Between Two Time Periods


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write a C Program to Calculate Difference Between Two Time Periods


  1. #include <stdio.h>
  2. struct Time
  3. {
  4. int seconds;
  5. int minutes;
  6. int hours;
  7. };
  8. void differenceBetweentwoTimePeriod(struct Time t1, struct Time t2, struct Time *diff);
  9.  
  10. int main()
  11. {
  12. struct Time startTime, stopTime, diff;
  13.  
  14. printf("Please enter start time: \n");
  15. printf("Enter hours, minutes and seconds respectively: ");
  16. scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
  17.  
  18. printf("Please enter stop time: \n");
  19. printf("Enter hours, minutes and seconds respectively: ");
  20. scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
  21.  
  22. // Calculateing the difference
  23. differenceBetweentwoTimePeriod(startTime, stopTime, &diff);
  24.  
  25. printf("\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
  26. printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
  27. printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
  28.  
  29. return 0;
  30. }
  31.  
  32. void differenceBetweentwoTimePeriod(struct Time start, struct Time stop, struct Time *diff)
  33. {
  34. if(stop.seconds > start.seconds){
  35. --start.minutes;
  36. start.seconds += 60;
  37. }
  38.  
  39. diff->seconds = start.seconds - stop.seconds;
  40. if(stop.minutes > start.minutes){
  41. --start.hours;
  42. start.minutes += 60;
  43. }
  44.  
  45. diff->minutes = start.minutes - stop.minutes;
  46. diff->hours = start.hours - stop.hours;
  47. }
  48.  

Result

C language techstudy.org

     





Related Topics




Popular Posts