Program to Calculate Difference Between Two Time Period.


Calculate Difference Between Two Time Periods. In this program, user is asked to enter two time periods and these two periods are stored in structure variables startTime and stopTime respectively.

Program:-

#include <iostream.h>
#include<conio.h>

struct TIME
{
  int seconds;
  int minutes;
  int hours;
};

void computeTimeDifference(struct TIME, struct TIME, struct TIME *);

void main()
{
clrscr();
    struct TIME t1, t2, difference;

    cout << "Enter start time." << endl;
    cout << "Enter hours, minutes and seconds respectively: ";
    cin >> t1.hours >> t1.minutes >> t1.seconds;

    cout << "Enter stop time." << endl;
    cout << "Enter hours, minutes and seconds respectively: ";
    cin >> t2.hours >> t2.minutes >> t2.seconds;

    computeTimeDifference(t1, t2, &difference);

    cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds;
    cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
    cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds;
    getch();
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *difference){

    if(t2.seconds > t1.seconds)
    {
--t1.minutes;
t1.seconds += 60;
    }

    difference->seconds = t1.seconds - t2.seconds;
    if(t2.minutes > t1.minutes)
    {
--t1.hours;
t1.minutes += 60;
    }
    difference->minutes = t1.minutes-t2.minutes;
    difference->hours = t1.hours-t2.hours;
}
Output:-
Program to Calculate Difference Between Two Time Period.

Post a Comment

0 Comments