घंटे, मिनट और सेकंड के साथ प्रारंभ और स्टॉप समय दर्ज करें। अंत में, हमें स्टार्ट और स्टॉप टाइम के बीच अंतर खोजने की जरूरत है।
शुरू और रुकने के समय के बीच अंतर . खोजने का तर्क नीचे दिया गया है -
while (stop.sec > start.sec){ --start.min; start.sec += 60; } diff->sec = start.sec - stop.sec; while (stop.min > start.min) { --start.hrs; start.min += 60; } diff->min = start.min - stop.min; diff->hrs = start.hrs - stop.hrs;
उदाहरण
स्टार्ट और स्टॉप टाइम के बीच अंतर खोजने का कार्यक्रम निम्नलिखित है -
#include <stdio.h> struct time { int sec; int min; int hrs; }; void diff_between_time(struct time t1, struct time t2, struct time *diff); int main(){ struct time start_time, stop_time, diff; printf("Enter start time. \n"); printf("Enter hours, minutes and seconds: "); scanf("%d %d %d", &start_time.hrs, &start_time.min, &start_time.sec); printf("Enter the stop time. \n"); printf("Enter hours, minutes and seconds: "); scanf("%d %d %d", &stop_time.hrs, &stop_time.min, &stop_time.sec); // Difference between start and stop time diff_between_time(start_time, stop_time, &diff); printf("\ntime Diff: %d:%d:%d - ", start_time.hrs, start_time.min, start_time.sec); printf("%d:%d:%d ", stop_time.hrs, stop_time.min, stop_time.sec); printf("= %d:%d:%d\n", diff.hrs, diff.min, diff.sec); return 0; } // Computes difference between time periods void diff_between_time(struct time start, struct time stop, struct time *diff){ while (stop.sec > start.sec) { --start.min; start.sec += 60; } diff->sec = start.sec - stop.sec; while (stop.min > start.min) { --start.hrs; start.min += 60; } diff->min = start.min - stop.min; diff->hrs = start.hrs - stop.hrs; }
आउटपुट
जब उपरोक्त प्रोग्राम को निष्पादित किया जाता है, तो यह निम्नलिखित परिणाम उत्पन्न करता है -
Enter start time. Enter hours, minutes and seconds: 12 45 57 Enter the stop time. Enter hours, minutes and seconds: 20 35 20 time Diff: 12:45:57 - 20:35:20 = -8:10:37