-
time converting
Hi! I am learning programming in C and I am trying to make a programme to convert the seconds in days, hours, minutes, and remaing seconds.
I have been discussed this problem earlier and got good response but I'm still unable to find the desired result.
I am using code like this
int s,m,h,d;
printf("Enter the number of seconds and press return:");
scanf("%d", &s);
// calculating the number of days
d=(((s/60)/60)/24);
printf("total days are %d",d);
Now I want to calculate the remaining seconds in hours, minutes, and seconds.
Can any one help me.
-
Code:
itsme@dreams:~/C$ cat timebrkdwn.c
#include <stdio.h>
#define SEC_PER_MIN 60
#define SEC_PER_HOUR 3600
#define SEC_PER_DAY 86400
int main(void)
{
int s, m, h, d;
printf("Enter the number of seconds and press return: ");
fflush(stdout);
scanf("%d", &s);
// Days
d = s / SEC_PER_DAY;
s %= SEC_PER_DAY;
// Hours
h = s / SEC_PER_HOUR;
s %= SEC_PER_HOUR;
// Minutes
m = s / SEC_PER_MIN;
s %= SEC_PER_MIN;
printf("Days: %d, Hours: %d, Minutes: %d, Seconds: %d\n", d, h, m, s);
return 0;
}
Code:
itsme@dreams:~/C$ ./timebrkdwn
Enter the number of seconds and press return: 923874
Days: 10, Hours: 16, Minutes: 37, Seconds: 54