输入年和月,输出该月的天数。c语言
时间: 2024-06-15 20:09:09 浏览: 190
在C语言中,可以通过以下方式来输入年和月,并输出该月的天数:
```c
#include <stdio.h>
int main() {
int year, month;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
// 判断是否为闰年
int isLeapYear = 0;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = 1;
}
// 判断每个月的天数
int days;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
days = isLeapYear ? 29 : 28;
break;
default:
printf("输入的月份无效!\n");
return 0;
}
printf("%d年%d月的天数为:%d\n", year, month, days);
return 0;
}
```
阅读全文