Convert integer (in seconds) to hr, min and sec

C program to convert a given integer (in seconds) to hours, minutes and seconds.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
    int sec, h, m, s, rem;

	printf("Enter seconds: ");
	scanf("%d", &sec);

	h = (sec / 3600);
	rem = sec % 3600;
	m = rem / 60;
	s = rem % 60;

	printf("\nH:M:S - %d:%d:%d",h,m,s);

	getch();
}

Output

Enter seconds: 4356

H:M:S - 1:12:36

Explanation

In the above program, time in seconds is taken as an input and stored in the variable named 'sec'. hours, minutes and seconds are stored in variables named 'h', 'm' and 's' respectively.

The approach used is as follows:
For the time in hours, divide the input by 3600(seconds in an hour) and obtain its quotient.
h = sec / 3600;

Now, obtain the remaining seconds after calculating the number of hours. This is done by dividing the input by 3600 and obtaining its remainder. This is done using modulo operator.
rem = sec % 3600;

Now, for the minutes, divide the remainder obtained above by 60(seconds in a minute) and obtain its quotient.
m = rem / 60;

Now, again obtain the remaining seconds after calculating the number of minutes and assign them to 's'. This is done by dividing the remaining seconds by 60 and obtaining its remainder. This is done using modulo operator.
s = rem % 60;

Let us understand the above process using an example,
Let, sec = 4356

h = (sec / 3600); h = 4356 / 3600 = 1
rem = sec % 3600; rem = 4356 % 3600 = 756
m = rem / 60; m = 756 / 60 = 12
s = rem % 60; s = 756 % 60 = 36