Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program to calculate the difference between two time periods
In C programming, calculating the difference between two time periods is a common problem that involves handling the borrowing mechanism similar to subtraction in arithmetic. This program takes two time periods as input and computes their difference in hours, minutes, and seconds format.
Syntax
struct time {
int hrs;
int min;
int sec;
};
void diff_between_time(struct time start, struct time stop, struct time *diff);
Algorithm
The logic to find the difference between start and stop time involves borrowing when the stop time component is greater than the start time component −
while (stop.sec > start.sec) {
--start.min;
start.sec += 60;
}
diff->sec = start.sec - stop.sec;
while (stop.min > start.min) {
--start.hrs;
start.min += 60;
}
diff->min = start.min - stop.min;
diff->hrs = start.hrs - stop.hrs;
Example
Following is the complete program to find difference between start and stop time −
#include <stdio.h>
struct time {
int sec;
int min;
int hrs;
};
void diff_between_time(struct time t1, struct time t2, struct time *diff);
int main() {
struct time start_time, stop_time, diff;
printf("Enter start time.
");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &start_time.hrs, &start_time.min, &start_time.sec);
printf("Enter the stop time.
");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &stop_time.hrs, &stop_time.min, &stop_time.sec);
// Calculate difference between start and stop time
diff_between_time(start_time, stop_time, &diff);
printf("\nTime Difference: %d:%d:%d - %d:%d:%d = %d:%d:%d
",
start_time.hrs, start_time.min, start_time.sec,
stop_time.hrs, stop_time.min, stop_time.sec,
diff.hrs, diff.min, diff.sec);
return 0;
}
// Function to compute difference between time periods
void diff_between_time(struct time start, struct time stop, struct time *diff) {
while (stop.sec > start.sec) {
--start.min;
start.sec += 60;
}
diff->sec = start.sec - stop.sec;
while (stop.min > start.min) {
--start.hrs;
start.min += 60;
}
diff->min = start.min - stop.min;
diff->hrs = start.hrs - stop.hrs;
}
Output
Enter start time. Enter hours, minutes and seconds: 12 45 57 Enter the stop time. Enter hours, minutes and seconds: 20 35 20 Time Difference: 12:45:57 - 20:35:20 = -8:10:37
Key Points
- The program uses a
struct timeto represent time with hours, minutes, and seconds. - The borrowing mechanism converts 1 minute to 60 seconds and 1 hour to 60 minutes when needed.
- Negative results indicate that the stop time is later than the start time.
Conclusion
This program demonstrates how to calculate time differences using structures and borrowing logic. The approach handles cases where individual components of stop time are greater than start time by converting higher units to lower units.
