Program to add two different times

Program to add two different times

  • Write a program to add two different times in C
  • Write a program to add two different times in C++
  • Write a program to add two different times in Python
  • Write a program to add two different times in PHP
  • Write a program to add two different times in Java
  • Write a program to add two different times in Java Script
  • Write a program to add two different times in C#

Explanation:

Adding two times involves summing their hours, minutes, and seconds separately, while handling any carryover. For instance:

  1. Add seconds. If the result exceeds 59, carry over the extra seconds as minutes.
  2. Add minutes. If the result exceeds 59, carry over the extra minutes as hours.
  3. Add hours. If you’re working in a 12-hour or 24-hour format, you might want to normalize the hours.

Program to add two different times

#include <stdio.h>

int main() {
    int hours1 = 2, minutes1 = 45;
    int hours2 = 1, minutes2 = 30;

    int totalHours, totalMinutes;

    // Add hours and minutes
    totalMinutes = minutes1 + minutes2;
    totalHours = hours1 + hours2;

    // Adjust for overflow in minutes
    if (totalMinutes >= 60) {
        totalHours += totalMinutes / 60;
        totalMinutes %= 60;
    }

    printf("Sum of times: %d hours and %d minutes\n", totalHours, totalMinutes);

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int hours1 = 2, minutes1 = 45;
    int hours2 = 1, minutes2 = 30;

    int totalHours, totalMinutes;

    // Add hours and minutes
    totalMinutes = minutes1 + minutes2;
    totalHours = hours1 + hours2;

    // Adjust for overflow in minutes
    if (totalMinutes >= 60) {
        totalHours += totalMinutes / 60;
        totalMinutes %= 60;
    }

    cout << "Sum of times: " << totalHours << " hours and " << totalMinutes << " minutes" << endl;

    return 0;
}

# Time inputs
hours1, minutes1 = 2, 45
hours2, minutes2 = 1, 30

# Add hours and minutes
total_minutes = minutes1 + minutes2
total_hours = hours1 + hours2

# Adjust for overflow in minutes
if total_minutes >= 60:
    total_hours += total_minutes // 60
    total_minutes %= 60

print(f"Sum of times: {total_hours} hours and {total_minutes} minutes")

<?php
// Time inputs
$hours1 = 2;
$minutes1 = 45;
$hours2 = 1;
$minutes2 = 30;

// Add hours and minutes
$totalMinutes = $minutes1 + $minutes2;
$totalHours = $hours1 + $hours2;

// Adjust for overflow in minutes
if ($totalMinutes >= 60) {
    $totalHours += intdiv($totalMinutes, 60);
    $totalMinutes %= 60;
}

echo "Sum of times: $totalHours hours and $totalMinutes minutes\n";
?>

public class Main {
    public static void main(String[] args) {
        int hours1 = 2, minutes1 = 45;
        int hours2 = 1, minutes2 = 30;

        int totalHours, totalMinutes;

        // Add hours and minutes
        totalMinutes = minutes1 + minutes2;
        totalHours = hours1 + hours2;

        // Adjust for overflow in minutes
        if (totalMinutes >= 60) {
            totalHours += totalMinutes / 60;
            totalMinutes %= 60;
        }

        System.out.println("Sum of times: " + totalHours + " hours and " + totalMinutes + " minutes");
    }
}

// Time inputs
let hours1 = 2, minutes1 = 45;
let hours2 = 1, minutes2 = 30;

// Add hours and minutes
let totalMinutes = minutes1 + minutes2;
let totalHours = hours1 + hours2;

// Adjust for overflow in minutes
if (totalMinutes >= 60) {
    totalHours += Math.floor(totalMinutes / 60);
    totalMinutes %= 60;
}

console.log(`Sum of times: ${totalHours} hours and ${totalMinutes} minutes`);

using System;

class Program {
    static void Main() {
        int hours1 = 2, minutes1 = 45;
        int hours2 = 1, minutes2 = 30;

        int totalHours, totalMinutes;

        // Add hours and minutes
        totalMinutes = minutes1 + minutes2;
        totalHours = hours1 + hours2;

        // Adjust for overflow in minutes
        if (totalMinutes >= 60) {
            totalHours += totalMinutes / 60;
            totalMinutes %= 60;
        }

        Console.WriteLine($"Sum of times: {totalHours} hours and {totalMinutes} minutes");
    }
}

List of All Programs