Calculate hours, minutes and seconds for the given seconds C++ program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Calculate hours, minutes and seconds for the given seconds in C++ programming using modulus operator
To show: How to calculate hours, minutes and seconds for the given seconds in C++ using modulus operator
// the modulus operator example. Inputs a number of seconds, and converts to hours, minutes and seconds.
#include <iostream>
using namespace std;
// define constants
#define SECS_PER_MIN 60
#define SECS_PER_HOUR 3600
void main(void)
{
unsigned int seconds, minutes, hours, secs_left, mins_left;
// prompting user to input the number of seconds
cout<<"Enter number of seconds: ";
// read and store the number
cin>>seconds;
// do some calculations using modulus operator
hours = seconds / SECS_PER_HOUR;
minutes = seconds / SECS_PER_MIN;
mins_left = minutes % SECS_PER_MIN;
secs_left = seconds % SECS_PER_MIN;
// print the result
cout<<seconds<<" seconds is equal to "<<hours<<" hours, "<<mins_left<<" minutes, "<<secs_left<<" seconds"<<endl;
return;
}
Output example:
Enter number of seconds: 553040
553040 seconds is equal to 153 hours, 37 minutes, 20 seconds
Press any key to continue . . .