Q:

C++ program to add seconds to the time

0

C++ program to add seconds to the time

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Most popular and Searched C++ solved programs with Explanation and Output

Similar questions


need a help?


find thousands of online teachers now
C++ program to check given date is in valid format... >>
<< C++ program to find Fibonacci number using differe...