×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to add two times

Given two times, we have to add them using C++ program.
[Last updated : February 28, 2023]

Adding two times

In this program, we will read two times (in hours, minutes and seconds) and calculate the total times that mean addition of two times.

Program to add two times in C++

#include <iostream>
using namespace std;

int main()
{
    int hh1, mm1, ss1; //to store first time
    int hh2, mm2, ss2; //to store second time
    int HH, MM, SS; //to store added time

    //read first time
    cout << "Enter first time:" << endl;
    cout << "Hours?: ";
    cin >> hh1;
    cout << "Minutes?: ";
    cin >> mm1;
    cout << "seconds?: ";
    cin >> ss1;

    //read second time
    cout << "Enter second time:" << endl;
    cout << "Hours?: ";
    cin >> hh2;
    cout << "Minutes?: ";
    cin >> mm2;
    cout << "seconds?: ";
    cin >> ss2;

    //add times
    SS = ss1 + ss2;
    MM = mm1 + mm2 + (SS / 60);
    HH = hh1 + hh2 + (MM / 60);
    MM = MM % 60; //remaining minutes
    SS = SS % 60; //remaining seconds

    //print time
    cout << "Added time is: ";
    cout << HH << " hour(s) " << MM << " minute(s) " << SS << " second(s)" << endl;

    return 0;
}

Output

Enter first time: 
Hours?: 10
Minutes?: 20
seconds?: 30
Enter second time:
Hours?: 15
Minutes?: 40
seconds?: 50
Added time is: 26 hour(s) 1 minute(s) 20 second(s)

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.