C++:Problem2

Problem:
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.

Objectives:
• Using arithmetic calculations
• Using division and mod
• Formatting source program
• Putting comments

Display:
Enter the elapsed time in seconds: 9630
02:40:30.
End of Program

Code:
#include
using namespace std;

int main ()
{
//declaration of variables
int seconds, minutes, hours, remain, elapsedtime;

//Prompt the user for input
cout << "Enter the elapsed time in seconds: ";
cin >> elapsedtime;

//Calculation
hours = elapsedtime / 3600;
remain = elapsedtime % 3600;
minutes = remain / 60;
seconds = remain % 60;

//Display output
if (hours < 10)
cout << "0" << hours <<":";
else
cout << hours << ":";

if (minutes < 10)
cout << "0" << minutes << ":";
else
cout << minutes << ":";

if (seconds < 10)
cout << "0" << seconds <<".";
else
cout << seconds <<".";

//closing
cout << "\nEnd of Program\n\n";
return 0;
}