C Program to Calculate Difference Between Two Time Periods
In this example, you’ll learn to calculate the difference between two time periods using the user-defined – differenceBetweenTimePeriod function.
Example: Calculate the Difference Between Two Time Periods
#include <stdio.h>
struct TIME
{
int seconds;
int minutes;
int hours;
};
void differenceBetweenTimePeriod(struct TIME t1, struct TIME t2, struct TIME *diff);
int main()
{
struct TIME startTime, stopTime, diff;
printf("Enter start time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &startTime.hours, &startTime.minutes, &startTime.seconds);
printf("Enter stop time: \n");
printf("Enter hours, minutes and seconds respectively: ");
scanf("%d %d %d", &stopTime.hours, &stopTime.minutes, &stopTime.seconds);
// Calculate the difference between the start and stop time period.
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTIME DIFFERENCE: %d:%d:%d - ", startTime.hours, startTime.minutes, startTime.seconds);
printf("%d:%d:%d ", stopTime.hours, stopTime.minutes, stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
void differenceBetweenTimePeriod(struct TIME start, struct TIME stop, struct TIME *diff)
{
if(stop.seconds > start.seconds){
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
if(stop.minutes > start.minutes){
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}Output
Enter start time: Enter hours, minutes and seconds respectively: 12 34 55 Enter stop time: Enter hours, minutes and seconds respectively:8 12 15 TIME DIFFERENCE: 12:34:55 - 8:12:15 = 4:22:40
In this program, the user is asked to enter two time periods and these two periods are stored in structure variables startTime and stopTime respectively.
Then, the function differenceBetweenTimePeriod calculates the difference between the time periods and the result is displayed in main() function without returning it (Using call by reference technique).
C program that calculates the difference between two time periods:
#include <stdio.h>
struct Time {
int hours;
int minutes;
int seconds;
};
int main() {
struct Time t1, t2, diff;
int seconds1, seconds2, totalSeconds;
// Input time values for t1
printf("Enter time 1 in hours, minutes, and seconds:\n");
scanf("%d %d %d", &t1.hours, &t1.minutes, &t1.seconds);
// Input time values for t2
printf("Enter time 2 in hours, minutes, and seconds:\n");
scanf("%d %d %d", &t2.hours, &t2.minutes, &t2.seconds);
// Calculate total seconds for t1 and t2
seconds1 = t1.hours * 3600 + t1.minutes * 60 + t1.seconds;
seconds2 = t2.hours * 3600 + t2.minutes * 60 + t2.seconds;
// Calculate difference between t1 and t2
totalSeconds = seconds2 - seconds1;
diff.hours = totalSeconds / 3600;
diff.minutes = (totalSeconds % 3600) / 60;
diff.seconds = totalSeconds % 60;
// Display the difference
printf("The difference between time 1 and time 2 is: %d hours, %d minutes, %d seconds\n", diff.hours, diff.minutes, diff.seconds);
return 0;
}
Explanation:
- We define a struct
Timeto hold the hours, minutes, and seconds values. - We then define three variables
t1,t2, anddiffof typeTimeto hold the input times and the time difference. - We also define three integer variables
seconds1,seconds2, andtotalSecondsto hold the total number of seconds fort1,t2, and the time difference, respectively. - We use
scanfto input the hours, minutes, and seconds values fort1andt2. - We calculate the total number of seconds for
t1andt2by multiplying the hours by 3600, the minutes by 60, and adding the seconds. We store the result inseconds1andseconds2, respectively. - We calculate the time difference by subtracting
seconds1fromseconds2. We store the result intotalSeconds. - We calculate the hours, minutes, and seconds for the time difference by dividing
totalSecondsby 3600, getting the quotient as hours, and then using the modulo operator to get the remaining minutes and seconds. We store the result indiff. - We then use
printfto display the time difference in hours, minutes, and seconds.
Note that this program assumes that the input times are valid and that t2 is greater than or equal to t1.