C Program to Calculate Difference Between Two Time Periods

Calculating the difference between two time periods is a common task in various applications, such as time tracking, event scheduling, and duration measurement. This article will cover three different C Program to Calculate Difference Between Two Time Periods to calculate the difference between two time periods using C. Each method will be explained in detail with example programs and outputs.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

  • Basic understanding of C programming.
  • Familiarity with structures in C.
  • A C compiler installed on your system (e.g., GCC).

Solution 1: Basic Approach Using Structures

1.1 Explanation

In this approach, we define a structure to represent a time period with hours, minutes, and seconds. We then create a function to calculate the difference between two time periods.

1.2 Program

C
#include <stdio.h>

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

// Function to calculate the difference between two time periods
struct Time calculateDifference(struct Time t1, struct Time t2) {
    struct Time diff;

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

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

    diff.hours = t1.hours - t2.hours;

    return diff;
}

int main() {
    struct Time t1, t2, diff;

    // Input first time period
    printf("Enter hours, minutes and seconds of first time period: ");
    scanf("%d %d %d", &t1.hours, &t1.minutes, &t1.seconds);

    // Input second time period
    printf("Enter hours, minutes and seconds of second time period: ");
    scanf("%d %d %d", &t2.hours, &t2.minutes, &t2.seconds);

    // Calculate the difference
    diff = calculateDifference(t1, t2);

    // Display the result
    printf("Difference: %d hours %d minutes %d seconds\n", diff.hours, diff.minutes, diff.seconds);

    return 0;
}

1.3 Output

C
Enter hours, minutes and seconds of first time period: 5 45 30
Enter hours, minutes and seconds of second time period: 3 20 15
Difference: 2 hours 25 minutes 15 seconds

Solution 2: Using Functions for Input and Output

2.1 Explanation

This approach uses separate functions for inputting time periods and displaying the difference. It modularizes the code, making it more readable and maintainable.

2.2 Program

C
#include <stdio.h>

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

// Function to input a time period
void inputTime(struct Time* t) {
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &t->hours, &t->minutes, &t->seconds);
}

// Function to calculate the difference between two time periods
struct Time calculateDifference(struct Time t1, struct Time t2) {
    struct Time diff;

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

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

    diff.hours = t1.hours - t2.hours;

    return diff;
}

// Function to display the difference
void displayDifference(struct Time diff) {
    printf("Difference: %d hours %d minutes %d seconds\n", diff.hours, diff.minutes, diff.seconds);
}

int main() {
    struct Time t1, t2, diff;

    // Input first time period
    printf("First time period:\n");
    inputTime(&t1);

    // Input second time period
    printf("Second time period:\n");
    inputTime(&t2);

    // Calculate the difference
    diff = calculateDifference(t1, t2);

    // Display the result
    displayDifference(diff);

    return 0;
}

Output

C
First time period:
Enter hours, minutes and seconds: 8 30 45
Second time period:
Enter hours, minutes and seconds: 2 45 50
Difference: 5 hours 44 minutes 55 seconds

Solution 3: Handling Negative Differences

3.1 Explanation

This approach extends the previous one to handle cases where the second time period is greater than the first, resulting in a negative difference. It ensures that the difference is always non-negative by swapping the time periods if necessary.

3.2 Program

C
#include <stdio.h>

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

// Function to input a time period
void inputTime(struct Time* t) {
    printf("Enter hours, minutes and seconds: ");
    scanf("%d %d %d", &t->hours, &t->minutes, &t->seconds);
}

// Function to calculate the difference between two time periods
struct Time calculateDifference(struct Time t1, struct Time t2) {
    struct Time diff;

    // Swap if t2 is greater than t1
    if ((t2.hours > t1.hours) || (t2.hours == t1.hours && t2.minutes > t1.minutes) || (t2.hours == t1.hours && t2.minutes == t1.minutes && t2.seconds > t1.seconds)) {
        struct Time temp = t1;
        t1 = t2;
        t2 = temp;
    }

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

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

    diff.hours = t1.hours - t2.hours;

    return diff;
}

// Function to display the difference
void displayDifference(struct Time diff) {
    printf("Difference: %d hours %d minutes %d seconds\n", diff.hours, diff.minutes, diff.seconds);
}

int main() {
    struct Time t1, t2, diff;

    // Input first time period
    printf("First time period:\n");
    inputTime(&t1);

    // Input second time period
    printf("Second time period:\n");
    inputTime(&t2);

    // Calculate the difference
    diff = calculateDifference(t1, t2);

    // Display the result
    displayDifference(diff);

    return 0;
}

Output

C
First time period:
Enter hours, minutes and seconds: 2 20 50
Second time period:
Enter hours, minutes and seconds: 6 30 20
Difference: 4 hours 9 minutes 30 seconds

Conclusion

Using C Program to Calculate Difference Between Two Time Periods is a fundamental task in many applications. This article presented three methods to achieve this using C: a basic approach, using functions for input and output, and handling negative differences. By understanding these techniques, you can effectively manage and manipulate time periods in your C programs. Each method offers different levels of complexity and flexibility, allowing you to choose the best approach for your specific needs.