-
Notifications
You must be signed in to change notification settings - Fork 306
/
Days.c
168 lines (165 loc) · 3.21 KB
/
Days.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<math.h>
bool leapyear(int y)
{
if(y%100==0)
{
if(y%400==0)
{
return 1;
}
else return 0;
}
else if(y%4==0)
{
return 1;
}
else return 0;
}
long calcleapyears(int y)
{
int cnt=0;
for(int i=0;i<=y;i++)
{
if(leapyear(i)==1)
{
cnt++;
}
else continue;
}
return cnt;
}
int calcdaysinmonth(int m, int y)
{
int sum=0;
for(int i=1;i<m;i++)
{
if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
{
sum+=31;
}
else if(i==4 || i==6 || i==9 || i==11)
{
sum+=30;
}
else if(i==2)
{
if(leapyear(y)==1)
{
sum+=29;
}
else if(leapyear(y)==0)
{
sum+=28;
}
}
}
return sum;
}
bool checkdate(int d,int m,int y)
{
if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12)
{
if(d==0 || d>31)
{
return 0;
}
else if(d>0 && d<32)
{
return 1;
}
}
else if(m==4 || m==6 || m==9 || m==11)
{
if(d==0 || d>30)
{
return 0;
}
else if(d>0 && d<31)
{
return 1;
}
}
else if(m==2)
{
if(leapyear(y)==1)
{
if(d==0 || d>29)
{
return 0;
}
else if(d>0 && d<30)
{
return 1;
}
}
else if(leapyear(y)==0)
{
if(d==0 || d>28)
{
return 0;
}
else if(d>0 && d<29)
{
return 1;
}
}
}
}
int main()
{
int d1,m1,y1,d2,m2,y2;
printf("Enter the earlier date d1(dd mm yyyy) : ");
scanf("%d %d %d",&d1,&m1,&y1);
printf("Enter the later date d2(dd mm yyyy) : ");
scanf("%d %d %d",&d2,&m2,&y2);
long long sum1=0,sum2=0,sum;
sum1=( (y1-calcleapyears(y1))*365 ) + (calcleapyears(y1)*366);
if(m1==0 || m1>12)
{
printf("The input month in date d1 is wrong!!!");
exit(0);
}
else if(m1>0 && m1<13)
{
sum1+=calcdaysinmonth(m1,y1);
}
if(checkdate(d1,m1,y1)==1)
{
sum1+=d1;
}
else if(checkdate(d1,m1,y1)==0)
{
printf("The input date in d1 is wrong!!!");
exit(0);
}
sum2=( (y2-calcleapyears(y2))*365 ) + (calcleapyears(y2)*366);
if(m2==0 || m2>12)
{
printf("The input month in date d2 is wrong!!!");
exit(0);
}
else if(m2>0 && m2<13)
{
sum2+=calcdaysinmonth(m2,y2);
}
if(checkdate(d2,m2,y2)==1)
{
sum2+=d2+1;
}
else if(checkdate(d2,m2,y2)==0)
{
printf("The input date in d2 is wrong!!!");
exit(0);
}
if(sum1>sum2)
{
printf("The dates entered are in wrong order!!!");
exit(0);
}
sum=sum2-sum1;
printf("The total number of days between two given dates is : %Ld \n",sum);
exit(0);
}