Home » C++ programs » C++ Most popular & searched 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).

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.

Consider the program:



#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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.