Time Difference Calculator C Program.

          Using Call by reference I have made a C program to calculate time difference between start time and end time. First we declare three integers in a structure ‘sec, min, hrs’ to store the timings in hours:minutes:seconds format. Then we declare a function Difference to which we are going to call later in the program.

   void Difference (struct TIME t1, struct TIME t2, struct TIME *diff);

  Then variables ‘t1,t2,diff’ are to be declared to store the values of start time and end time. First we will ask the user to enter the start time and then store it in t1 variables. Then we ask the user the end time and store it in variable t2. When we have got both the times we then call the function Difference.

   Difference (t1,t2,&diff);

   In difference function we use If…else condition to check which one of the time is greater so as to subtract smaller value from greater one by one. The value of hours is checked to find the greatest amongst it and then subtracted. Similarly rest of the time difference is calculated and the difference is displayed on the command prompt.

Here is the code of Difference function:

   void Difference (struct TIME t1, struct TIME t2, struct TIME *differ){
      if(t2.hrs>t1.hrs){
         differ->hrs=t2.hrs-t1.hrs;}
      else differ->hrs=t1.hrs-t2.hrs;

      if(t2.min>t1.min){
         differ->min=t2.min-t1.min;}
      else differ->min=t1.min-t2.min;

      if (t2.sec>t1.sec){
         differ->sec=t2.sec-t1.sec;}
      else differ->sec=t1.sec-t2.sec;}

Output:-

Selection_035

Download the Source Code here:-

Download periodic table project


Posted

in

by

Tags:

Comments

3 responses to “Time Difference Calculator C Program.”

  1. Anonymous Avatar
    Anonymous

    This is incorrect. This would make 9:00 to 1:00 become 8 hours instead of 4 hours difference.

    Like

    1. Shirish Kadam Avatar

      Its a Lap time difference calculator and is in global standard format of 24hrs and not 12hrs so the result is correct…! 🙂 So, in 24 hrs format time never goes from 9:00 to 1:00 but from 1:00 to 9:00.

      I know the else condition can be confusing but it just ensures that always greater value gets subtracted from smaller!

      Like

      1. Luis Avatar
        Luis

        In the given example, the correct answer would be 3:18:17. The difference function is not that simple.

        Like

Leave a Comment:

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

%d bloggers like this: