forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DaysInMonth.c
75 lines (59 loc) · 1.74 KB
/
DaysInMonth.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
/*
This program computes the number of days in a user-specified month
*/
# include <stdio.h>
// The months in a year represented by numbers 1 - 12
# define JANUARY 1
# define FEBRUARY 2
# define MARCH 3
# define APRIL 4
# define MAY 5
# define JUNE 6
# define JULY 7
# define AUGUST 8
# define SEPTEMBER 9
# define OCTOBER 10
# define NOVEMBER 11
# define DECEMBER 12
// The start date of the first calendar
# define CALENDAR_START 1582
int main(void)
{
int year, month;
// Get date information
printf("\nEnter year: ");
scanf("%d", &year);
printf("Enter month: ");
scanf("%d", &month);
// Validate user input
if( (year < CALENDAR_START) || (month < 1 ) || (month > 12))
{
printf("Bad Input: %d %d\n", year, month);
system("pause");
return;
}
// Determine if the year is a leap year, 0 is false and 1 is true, default value is 0 for false
int isDivisibleBy4 = 0, isDivisibleBy100 = 0, isDivisibleBy400 = 0;
int isLeapYear = 0;
if(year % 4 == 0)
isDivisibleBy4 = 1;
if(year % 100 == 0)
isDivisibleBy100 = 1;
if(year % 400 == 0)
isDivisibleBy400 = 1;
if(isDivisibleBy4 == 1 || isDivisibleBy100 == 1 || isDivisibleBy400 == 1)
isLeapYear = 1;
// Use month and year information to determine number of days
int days = 0;
if(month == JANUARY || month == MARCH || month == MAY || month == JULY || month == AUGUST || month == OCTOBER || month == DECEMBER)
days = 31;
else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
days = 30;
else if(month == FEBRUARY)
if(isLeapYear == 1)
days = 29;
else
days = 28;
printf("Month %d in year %d has %d days\n", month,year,days);
system("pause");
}