C Online Compiler
Example: Basic Time Adder in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Basic Time Adder #include <stdio.h> int main() { // Step 1: Declare variables for input and result int hour1, minute1; int hour2, minute2; int totalHours, totalMinutes; // Step 2: Get the first time duration from the user printf("Enter first duration (hours minutes): "); scanf("%d %d", &hour1, &minute1); // Step 3: Get the second time duration from the user printf("Enter second duration (hours minutes): "); scanf("%d %d", &hour2, &minute2); // Step 4: Add minutes and handle overflow totalMinutes = minute1 + minute2; totalHours = hour1 + hour2; // Step 5: Adjust hours if totalMinutes is 60 or more totalHours += totalMinutes / 60; // Add whole hours from minutes totalMinutes %= 60; // Remaining minutes // Step 6: Print the result printf("Sum of durations: %d hours %d minutes\n", totalHours, totalMinutes); return 0; }
Output
Clear
ADVERTISEMENTS