# secs into hours mins and remainin secs

• 01-12-2008
manzoor
secs into hours mins and remainin secs
Can anyone tell me a good algorithm on how to convert seconds into hours, minutes and remaining secs ?

I have this source code, but when you enter a bigger value it gets slowed..

Code:

#include <iostream>

using namespace std;

int main(void)
{
int secondsInput, seconds, hours = 0, minutes = 0;
cout << "enter number of seconds: ";
cin >> secondsInput;

seconds = secondsInput;

int hrs = 3600;
int mins = 60;

for ( int x = 1; x <= secondsInput; x++ ) {

if ( x == hrs) {
hours += 1;
seconds -= 3600;
hrs += 3600;
}
}

for ( int y = 1; y <= seconds; y++ ) {

if ( y == mins ) {
minutes += 1;
mins += 60;
}
}

int z = secondsInput - (( 3600 * hours ) +  ( 60 * minutes )); // z = number of seconds

cout << "\n" << secondsInput << " equals to " << hours << " hours " << minutes << " minutes " << z << " seconds";
return 0;
}

Thanks
• 01-12-2008
Elysia
Code:

int nSeconds = 3930;
int nMinutes = nSeconds / 60;
nHours = nMinutes / 60;
nSeconds -= nMinutes * 60;
nMinutes -= nHours * 60;

• 01-12-2008
ZuK
something simple like this should do
Code:

int main() {
int secs = 1234567;
int h,m,s;
h = secs / 3600;
m = (secs - h * 3600) / 60;
s = secs - h*3600 - m*60;
}

Kurt