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