Convert time in seconds in C++
cin - Question 16
In this question, we will see how to input time in hours, minutes and seconds in C++ programming using the cin statement and print the total time only in seconds. To know more about cin statement click on the cin statement lesson.
Q16) Write a program in C++ to input time in hours, minutes and seconds and print the total time only in seconds.
Hour: 1
Minutes: 10
Seconds: 6
Total Seconds: 4206
Program
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int h,m,s,ts;
cout<<"Enter hours ";
cin>>h;
cout<<"Enter minutes ";
cin>>m;
cout<<"Enter seconds ";
cin>>s;
ts=(h*3600)+(m*60)+s;
cout<<"Total time in seconds="<<ts;
return 0;
}
Output
Enter hours 1 Enter minutes 20 Enter seconds 30 Total time in seconds=4830