# Thread: Algo for create hours and minutes with given seconds

1. ## Algo for create hours and minutes with given seconds

Hi...this (semplified) code wait till I press a key, then it looks for the seconds elapsed.

But I want to divide them into hours/minute/seconds and not only seconds.. the algo I've wrote it's wrong (but it seems ok to me )

... can u help me ?

Code:
```#include <stdio.h>
#include <time.h>

int main ()

{
time_t start,end, timer;
char Input ;
double dif;
int ore;  //hours
int minuti; //minutes
int secondi;  //seconds

timer=time(NULL);
time (&start);
printf("Workunit Started   --> %s",asctime(localtime(&timer)));
gets (Input);
time (&end);
dif = difftime (end,start);
timer=time(NULL);
printf("Workunit Completed --> %s\n",asctime(localtime(&timer)));

// that's wrong !!!
ore = dif/216000;
minuti = (dif-(ore*216000))/3600;
secondi = dif-((ore*216000)-(minuti*3600));

printf ("Time elapsed : %d:%d:%d \n", ore,minuti,secondi );

system("PAUSE");

return 0;
}``` 2. If your algorithm doesn't work, try it using a pen and paper. 3. Doing an average system may help.

Especially for the minutes etc... It's not that hard of an algorithm, it just takes a while to write and understand.

Though, jotting it down may help [which I haven't myself] I like playing around with code until I understand how it works and how I did it. I can always give some recommendations of how this code works if you like.

- Stack Overflow 4. What is the purpose of timer in your code? It doesn't do anything, and you never actually use it. Why not just:
Code:
```        start = time( NULL );
/* do stuff */
end = time( NULL );
difference = difftime( end, start );
/* calculate */
/* display */```
Quzah. 5. ok i'm trying to figure out why it doesnt work ps: for Quzah... I need timer in the code I've cut (I preferred to not post a very long code) 6. Code:
```somenumber = passednumberofseconds
somenumber % 60 = the "left over" seconds, this should be stored in a seconds counter
somenumber / 60 = number of minutes
number of minutes % 60 = "left over minutes", store this in some minutes counter
number of minutes / 60 = hours```
This is like the "make correct change" exercise people commonly do. You are in effect "making correct change" from a given number of seconds (cents).

Quzah. 7. thanx to all

anyway I got it, it was a sign mistake, a - for a +

this works

Code:
```  ore = dif/3600;
minuti = (dif-(ore*3600))/60;
secondi = dif-((ore*3600)+(minuti*60));``` Popular pages Recent additions 