assignment-p-04.c
Problem Statement
assignment-p-04.c
Write a C program that takes an integer input representing a month (1 to 12) and a year. Use a switch statement to display the number of days in that month, considering leap years.
Source Code
c
#include <stdio.h>
int main()
{
int month, year, days;
printf("Enter the month (1 to 12) and year: ");
scanf("%d %d", &month, &year);
switch (month)
{
case 1: // jan
case 3: // mar
case 5: // may
case 7: // july
case 8: // aug
case 10: // oct
case 12: // dec
days = 31;
break;
case 4: // apr
case 6: // jun
case 9: // sep
case 11: // nov
days = 30;
break;
case 2: // feb
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
days = 29;
}
else
{
days = 28;
}
break;
default:
printf("\nYou entered something wrong.");
return 0;
}
printf("\nNumber of days: %d", days);
return 0;
}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
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
● Author - Amit Dutta · Updated - 12 Dec 2025