Time Passes


General Statement: Read 2 times (based on a 24-hour clock) and determine how much time has elapsed from time #1 to time #2.

Input: The first line of the data set for this problem is an integer that represents the number of data sets that follow. Each data set is on a separate line and consists of 2 times in the form hh:mm:ss. Values less than 10 have a leading zero.

Output: Output the number of hours, the number of minutes, and the number of seconds that have passed from the first to the second time.
Label each time unit
All letters are upper case.
The output is to be formatted exactly like that for the sample output given below.

Assumptions: This is a 24-hour clock.
Hours are in the range 0..23. Minutes and seconds are in the range 0..59.

Discussion:
If time #2 is “before” time #1, it is the following day.

Sample Input:
3
10:03:43 15:00:58
07:25:00 20:52:14
16:30:30 05:40:10

Sample Output:
4 HOURS 57 MINUTES 15 SECONDS
13 HOURS 27 MINUTES 14 SECONDS
13 HOURS 9 MINUTES 40 SECONDS

Solutions :

#include <stdio.h>

int main ()
{
    int dataset;
    scanf ("%d", &dataset);

    while ( dataset-- ) {
        int time1_h;
        int time1_m;
        int time1_s;

        scanf ("%d:%d:%d", &time1_h, &time1_m, &time1_s);

        int time2_h;
        int time2_m;
        int time2_s;

        scanf ("%d:%d:%d", &time2_h, &time2_m, &time2_s);

        int time1_in_seconds = (time1_h * 3600) +
        (time1_m * 60) + time1_s;

        int time2_in_seconds = (time2_h * 3600) +
        (time2_m * 60) + time2_s;

        int seconds_diff;

        if ( time1_in_seconds > time2_in_seconds )
            seconds_diff = (24 * 3600) - (time1_in_seconds
            - time2_in_seconds);

        else
            seconds_diff = time2_in_seconds - time1_in_seconds;

        printf ("%d HOURS ", seconds_diff / 3600 );
        printf ("%d MINUTES ", (seconds_diff % 3600) / 60);
        printf ("%d SECONDS\n", (seconds_diff % 3600) % 60);
    }

    return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s