BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Add two Times entered by User

Last Updated: December 26, 2017 by Chaitanya Singh | Filed Under: C++ Programs

This program takes two times as input and displays the total time after adding both the entered times.

Example: Program to add two times

#include <iostream>
using namespace std;

int main()
{
	//for first time
	int hour1,minute1,second1;
	//for second time
	int hour2,minute2,second2;
	//for the total(sum) time
	int hour,minute,second;

	//taking the input from user
	cout<<"***Enter first time***"<<endl;
	cout<<"Hours: "; cin>>hour1;
	cout<<"Minutes: "; cin>>minute1;
	cout<<"Seconds: "; cin>>second1;

	//taking the input from user
	cout<<"***Enter second time***"<<endl;
	cout<<"Hours: "; cin>>hour2;
	cout<<"Minutes: "; cin>>minute2;
	cout<<"Seconds: "; cin>>second2;

	//adding the entered times
	second=second1+second2;
	minute=minute1+minute2+(second/60);
	hour=hour1+hour2+(minute/60);
	minute=minute%60;
	second=second%60;

	//displaying total time
	cout<<"Total Time is: "<<hour<<" hours "<<minute<<" minutes "<<second<< " seconds";

	return 0;
}

Output:

***Enter first time***
Hours: 5
Minutes: 10
Seconds: 45
***Enter second time***
Hours: 6
Minutes: 40
Seconds: 45
Total Time is: 11 hours 51 minutes 30 seconds

Top Related Articles:

  1. C++ Program to add two numbers
  2. C++ Program to Find and display the Transpose of a Matrix
  3. C++ Program to Find largest element in an array
  4. C++ Program to Add two Matrices
  5. C++ Program to Find Second Smallest Element in an Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap