-
Notifications
You must be signed in to change notification settings - Fork 118
/
convert_secs_to_days_hours_mins_secs.c
83 lines (72 loc) · 2.66 KB
/
convert_secs_to_days_hours_mins_secs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*******************************************************************************
*
* Program: Convert Seconds To Days, Hours, Minutes and Seconds
*
* Description: Program to convert a total number of seconds into the equivalent
* number of days, hours, minutes and seconds using C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=bQgRTianPRI
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
// Algorithm Explained
// -------------------
//
// If we divide the total number of seconds by 60 (the number of seconds in a
// minute) then the quotient will be the total number of minutes and the
// remainder will be the number of seconds left over after accounting for the
// number of minutes.
//
// e.g. 200,000 / 60 = 3,333 remainder 20
//
// If we take that total number of minutes and divide it by 60 (the number of
// minutes in an hour) then the quotient will be the total number of hours and
// the remainder will be the number of minutes left over after accounting for
// the number of hours.
//
// e.g. 3,333 / 60 = 55 remainder 33
//
// And if we take that total number of hours and divide it by 24 (the number of
// hours in a day) then the quotient will be the total number of days and the
// remainder will be the number of hours left over after accounting for the
// number of days.
//
// e.g. 55 / 24 = 2 remainder 7
//
// So 200,0000 seconds is 2 days, 7 hours, 33 minutes, and 20 seconds
#include <stdio.h>
int main(void)
{
// stores the total initial amount of seconds
int total_seconds = 0;
// Store the total number of whole minutes and hours as used in the above
// calculations
int total_minutes = 0;
int total_hours = 0;
// Store the equivalent number of days, hours, minutes and seconds
int days = 0;
int hours = 0;
int minutes = 0;
int seconds = 0;
// Prompt the user for the total number of seconds and store the input
printf("Total Seconds: ");
scanf("%d", &total_seconds);
// Find the remaining number of seconds (seconds) and total number of minutes
// (total_minutes)
seconds = total_seconds % 60;
total_minutes = total_seconds / 60;
// Find the remaining number of minutes (minutes) and total number of hours
// (total_hours)
minutes = total_minutes % 60;
total_hours = total_minutes / 60;
// Find the remaining number of hours (hours) and total days (days)
hours = total_hours % 24;
days = total_hours / 24;
// Output the resulting days, hours, minutes and seconds
printf("%d days\n", days);
printf("%d hours\n", hours);
printf("%d minutes\n", minutes);
printf("%d seconds\n", seconds);
return 0;
}