Home » C++ programs » C++ Most popular & searched programs

C++ program to add 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)





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.