C++ program that prompts the user to input the elapsed time for an event in seconds then outputs the elapsed time in hours, minutes, and seconds
Posted by Samath
Last Updated: April 06, 2022

Write a C++ program that prompts the user to input the elapsed time for an event in seconds. 
 
 The program then outputs the elapsed time in hours, minutes,
 and seconds. (For example, if the elapsed time is 9630 seconds, 
 then the output is 2:40:30.)

Code:

#include <iostream>

using namespace std;

int main()
{
    int secElapsed, hours, min, sec;
    
    const int secPerMin = 60;
    const int secPerHour = 60 * secPerMin;
    
    cout << "Enter the number of seconds elapsed: ";
    cin >> secElapsed;
    
    hours = secElapsed / secPerHour;
    secElapsed = secElapsed % secPerHour;
    min = secElapsed / secPerMin;
    sec = secElapsed % secPerMin;
    
    cout << hours << ":" << min << ":" << sec << endl;
    return 0;
}