- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program to calculate the difference between two time periods
Enter the start and stop time with hours, minutes and seconds. Finally, we need to find the difference between start and stop time.
The logic to find the difference between start and stop time is given below −
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 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); // Difference between start and stop time diff_between_time(start_time, stop_time, &diff); printf("
time Diff: %d:%d:%d - ", start_time.hrs, start_time.min, start_time.sec); printf("%d:%d:%d ", stop_time.hrs, stop_time.min, stop_time.sec); printf("= %d:%d:%d
", diff.hrs, diff.min, diff.sec); return 0; } // Computes 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
When the above program is executed, it produces the following result −
Enter start time. Enter hours, minutes and seconds: 12 45 57 Enter the stop time. Enter hours, minutes and seconds: 20 35 20 time Diff: 12:45:57 - 20:35:20 = -8:10:37
- Related Articles
- Difference between two given time periods in C++
- C++ Program to Calculate Difference Between Two Time Period
- Java Program to Calculate the difference between two sets
- How can we calculate the difference between two time values in MySQL?
- C program to calculate distance between two points
- Java Program to get the difference between two time zones by seconds
- Program to calculate the area between two Concentric Circles in C++?
- How to calculate the difference between time in different MySQL columns?
- C# Program to return the difference between two sequences
- C# program to list the difference between two lists
- C# Program to get the difference between two dates
- C Program to calculate the Round Trip Time (RTT)
- How to calculate the difference between two dates in JavaScript?
- Swift Program to Calculate Distance Between Two Points
- C# Program to determine the difference in hours between two dates

Advertisements