-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeConversions.c
60 lines (50 loc) · 1.22 KB
/
timeConversions.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
#include <stdio.h>
#include <stdlib.h>
int timeToSeconds(int hh, int mm, int ss)
{
int sec;
sec = hh * 60 * 60 + mm * 60 + ss;
return sec;
}
int* secondsToTime(int seconds)
{
int *time = (int*)malloc(3*sizeof(int));
int hh = seconds / (60 * 60);
int mm = seconds / 60 - hh * 60;
int ss = seconds - (hh * 60 *60 ) - (mm*60);
time[0] = hh;
time[1] = mm;
time[2] = ss;
return time;
}
int main()
{
int seconds =0;
int *time;
short int choice;
printf("\n1.Convert time to seconds\n2.Convert seconds to time\n");
printf("\nEnter your choice: ");
scanf("%hu", &choice);
switch(choice)
{
case 1: {
int hh, mm ,ss;
printf("Enter time in hh: mm: ss ");
scanf("%d %d %d",&hh, &mm, &ss);
seconds = timeToSeconds(hh, mm, ss);
break;
}
case 2:{
int seconds;
printf("Enter seconds:");
scanf("%d", &seconds);
time = secondsToTime(seconds);
break;
}
}
if(choice==1)
printf("Time in seconds: %d", seconds);
else
printf("Time in HH : MM : SS \t %d :%d :%d \n", time[0], time[1], time[2]);
return 0;
}