c语言,用户输入一个月份和一个年份,然后判断并输出这个月份是否有31天,考虑到闰年的情况。
时间: 2024-10-18 07:24:50 浏览: 82
在C语言中,你可以通过编写如下的程序来实现这个功能。首先,你需要创建一个函数`is_leap_year()`来检查给定的年份是否是闰年,然后在主函数里获取用户的月份和年份输入,并调用该函数。接下来判断每个月份是否有31天:
```c
#include <stdio.h>
// 判断是否为闰年
int is_leap_year(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
return 1; // 是闰年
}
else {
return 0; // 非闰年
}
}
// 判断月份是否允许有31天
void check_days_in_month(int month, int year) {
switch(month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("%d年%d月有31天。\n", year, month);
break;
case 4: case 6: case 9: case 11:
printf("%d年%d月有30天。\n", year, month);
break;
default:
if(is_leap_year(year) && month == 2) { // 二月闰年有29天
printf("%d年%d月有29天。\n", year, month);
} else {
printf("%d年%d月有28天。\n", year, month);
}
break;
}
}
int main() {
int month, year;
printf("请输入月份(1-12): ");
scanf("%d", &month);
printf("请输入年份: ");
scanf("%d", &year);
check_days_in_month(month, year);
return 0;
}
```
阅读全文
相关推荐


















