10. Time conversion - the solution
#include <stdio.h>
/*
Load time in seconds and print in hh:mm:ss format
*/
int main()
{
int time,hh,mm,ss;
printf("Enter the time in seconds\n");
scanf("%d",&time); //7345s
/*ss=25, Dividing by 60 would divide 7320(60*122) and the result of division would be 122.
The remainder of the division is then 7345-7320= 25, which is obtained by the % operator, therefore, 7345 % 60 = 25*/
ss=time % 60;
mm = time / 60; //mm=122
/*In these minutes, hours can be extracted because 120 min is 2*60 = 120, ie. 2 hours, then only 2 minutes will remain*/
hh = mm / 60; //hh=2
mm = mm % 60; //mm=2, this could also be obtained by subtracting ie. mm= mm-hh*60 =122 -2*60 = 122-120 = 2
printf("%2d h : %2d m : %2d s", hh, mm, ss);
return 0;
}
/*
Load time in seconds and print in hh:mm:ss format
*/
int main()
{
int time,hh,mm,ss;
printf("Enter the time in seconds\n");
scanf("%d",&time); //7345s
/*ss=25, Dividing by 60 would divide 7320(60*122) and the result of division would be 122.
The remainder of the division is then 7345-7320= 25, which is obtained by the % operator, therefore, 7345 % 60 = 25*/
ss=time % 60;
mm = time / 60; //mm=122
/*In these minutes, hours can be extracted because 120 min is 2*60 = 120, ie. 2 hours, then only 2 minutes will remain*/
hh = mm / 60; //hh=2
mm = mm % 60; //mm=2, this could also be obtained by subtracting ie. mm= mm-hh*60 =122 -2*60 = 122-120 = 2
printf("%2d h : %2d m : %2d s", hh, mm, ss);
return 0;
}