C Online Compiler
Example: Basic Time Difference Calculator in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Basic Time Difference Calculator #include <stdio.h> int main() { // Step 1: Declare variables for input and result int startHour, startMinute; int endHour, endMinute; int diffHour, diffMinute; // Step 2: Get the start time from the user printf("Enter start time (hours minutes): "); scanf("%d %d", &startHour, &startMinute); // Step 3: Get the end time from the user printf("Enter end time (hours minutes): "); scanf("%d %d", &endHour, &endMinute); // Step 4: Calculate difference in minutes first int totalStartMinutes = startHour * 60 + startMinute; int totalEndMinutes = endHour * 60 + endMinute; // Step 5: Calculate total difference in minutes int totalDiffMinutes = totalEndMinutes - totalStartMinutes; // Step 6: Convert total difference minutes back to hours and minutes diffHour = totalDiffMinutes / 60; diffMinute = totalDiffMinutes % 60; // Step 7: Print the result printf("Time difference: %d hours %d minutes\n", diffHour, diffMinute); return 0; }
Output
Clear
ADVERTISEMENTS