Home C++ Tutorial C++ Program to Calculate Difference Between Two Time Period

C++ Program to Calculate Difference Between Two Time Period

by Anup Maurya
33 minutes read

In this article, we will learn how to write a C++ Program to Calculate Difference Between Two Time Period. We will also display the program output.

#include <iostream>
using namespace std;

// Structure to represent a time period
struct Time {
    int hours;
    int minutes;
    int seconds;
};

// Function to calculate the difference between two time periods
Time differenceBetweenTimePeriods(Time startTime, Time endTime) {
    Time diff;

    if (endTime.seconds > startTime.seconds) {
        --startTime.minutes;
        startTime.seconds += 60;
    }

    diff.seconds = startTime.seconds - endTime.seconds;

    if (endTime.minutes > startTime.minutes) {
        --startTime.hours;
        startTime.minutes += 60;
    }

    diff.minutes = startTime.minutes - endTime.minutes;
    diff.hours = startTime.hours - endTime.hours;

    return diff;
}

int main() {
    Time startTime, endTime, diff;

    cout << "Enter start time:" << endl;
    cout << "Hours: ";
    cin >> startTime.hours;
    cout << "Minutes: ";
    cin >> startTime.minutes;
    cout << "Seconds: ";
    cin >> startTime.seconds;

    cout << "Enter end time:" << endl;
    cout << "Hours: ";
    cin >> endTime.hours;
    cout << "Minutes: ";
    cin >> endTime.minutes;
    cout << "Seconds: ";
    cin >> endTime.seconds;

    diff = differenceBetweenTimePeriods(startTime, endTime);

    cout << "Difference: " << diff.hours << " hours, " << diff.minutes << " minutes, " << diff.seconds << " seconds." << endl;

    return 0;
}

Explanation

In this program, we first define a Time structure to represent the time period. The structure has three fields: hours, minutes, and seconds. These fields hold the values for the respective time components.

The program then defines a function named differenceBetweenTimePeriods() that takes two Time structures as parameters: startTime and endTime. This function is responsible for calculating the difference between the two time periods.

Inside the differenceBetweenTimePeriods() function, we perform the necessary calculations to determine the difference in hours, minutes, and seconds. If the end time has a higher value for seconds or minutes than the start time, we adjust the values accordingly to ensure accurate calculations.

In the main() function, we take user input for the start time and end time periods. The user is prompted to enter the hours, minutes, and seconds for both the start and end times.

After obtaining the input, we call the differenceBetweenTimePeriods() function passing in the start and end time periods as arguments. The function calculates the difference and returns a new Time structure containing the difference.

Finally, we display the difference in hours, minutes, and seconds using the cout statements.

Output

When you run the program and provide inputs for the start and end time, you will get an output similar to the following:

Enter start time:
Hours: 2
Minutes: 30
Seconds: 45
Enter end time:
Hours: 3
Minutes: 40
Seconds: 30
Difference: 1 hour(s), 9 minute(s), 45 second(s).

This output indicates that the difference between the start time and end time is 1 hour, 9 minutes, and 45 seconds.

I hope this article helps you understand how to calculate the difference between two time periods using C++. If you have any further questions, feel free to ask!

related posts

Leave a Comment