-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.c
103 lines (90 loc) · 2.35 KB
/
Time.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
struct time{
int hr,sec,min;
};
void read(struct time *t){
printf("Enter the time in hour minute second format\n");
scanf("%d%d%d",&t->hr,&t->min,&t->sec);
}
void display(struct time* t){
printf("Time is %d\t%d\t%d\n",t->hr,t->min,t->sec);
}
void update(struct time* t){
t->sec++;
if(t->sec>=60){
t->min++;
t->sec=t->sec%60;
if(t->min>=60){
t->hr++;
t->min=t->min%60;
if(t->hr>=24){
t->min=t->hr=t->sec=0;
}
}
}
}
void add(struct time* t1,struct time* t2,struct time* t3){
(t3->sec)=(t1->sec)+(t2->sec);
(t3->min)=(t1->min)+(t2->min);
(t3->hr)=(t1->hr)+(t2->hr);
if(t3->sec>=60){
t3->min++;
t3->sec=t3->sec%60;
if(t3->min>=60){
t3->hr++;
t3->min=t3->min%60;
if(t3->hr>=24){
t3->min=t3->hr=t3->sec=0;
}
}
}
if(t3->min>=60){
t3->hr++;
t3->min=t3->min%60;
if(t3->hr>=24){
t3->min=t3->hr=t3->sec=0;
}
}
if(t3->hr>=24){
t3->min=t3->hr=t3->sec=0;
}
}
void main(){
struct time t1,t2,t3,t4;
int choice;
while(1){
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice){
case 1:
printf("enter the time 1");
read(&t1);
printf("entered time is\n");
display(&t1);
printf("enter the time 2");
read(&t2);
printf("entered time is\n");
display(&t2);
break;
case 2:
printf("enter the time to be updated \n");
read(&t3);
printf("before update\n");
display(&t3);
update(&t3);
printf("after update\n");
display(&t3);
break;
case 3:
add(&t1,&t2,&t4);
printf("The sum of time t1 and t2 is ");
display(&t4);
break;
case 4:
exit(0);
default:
printf("enter the valid choice\n");
}
}
}