×

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 seconds to the time

Learn: How to add seconds in given time and print the time in HH:MM:SS format using C++ program, here we will add seconds in time (with using all conditions like time truncating etc).
[Last updated : February 26, 2023]

Adding the given seconds to the time

As we know that now a days, there are lot of applications are used on the basis of time calculation so that sometime we need to perform operations on time.

In this program, we are going to learn adding seconds to the time.

C++ code to add the given seconds to the time

#include <iostream>
using namespace std;

void addSecond(int* hh, int* mm, int* ss)
{
    if (*ss == 59) {
        if (*mm == 59) {
            cout << endl
                 << "hh: " << *hh << " mm: " << *mm << " ss:" << *ss << endl;

            if (*hh == 23)
                *hh = 0;
            else
                (*hh)++;

            *mm = 0;
            *ss = 0;
        }
        else {
            (*mm)++;
            *ss = 0;
        }
    }
    else {
        (*ss)++;
    }
}

int main()
{
    int hh = 23, mm = 59, ss = 59;

    cout << "Time before adding second(s): " << hh << ":" << mm << ":" << ss << endl;
    addSecond(&hh, &mm, &ss);
    cout << "Time After adding second(s) : " << hh << ":" << mm << ":" << ss << endl;

    return 0;
}

Output

Time before adding second(s): 23:59:59

hh: 23 mm: 59 ss:59 
Time After adding second(s) : 0:0:0

In above example, we have added one second to 23:59:59 in 24 hours clock. Thus, the output is 0:0:0, it is in next day.

Related Programs

Comments and Discussions!

Load comments ↻





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