输入年月输出天数 c语言
时间: 2023-12-14 19:03:12 浏览: 44
根据提供的两个引用,可以给出以下回答:
输入年月输出天数的C语言程序可以根据不同的年份和月份来判断该月有多少天。其中,闰年的判断可以使用ifLeapYear函数,该函数可以判断输入的年份是否为闰年。如果是闰年,则该年2月份有29天,否则2月份只有28天。根据不同的月份,可以使用两个数组month1和month2来存储每个月的天数,然后根据输入的天数来判断是哪个月份。具体实现可以参考以下代码:
```c
#include <stdio.h>
#include <stdbool.h>
bool ifLeapYear(int year) {
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
return true;
else
return false;
}
int main() {
int year, month, date, i;
int month1[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int month2[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool flag;
printf("请输入年份和月份:");
scanf("%d%d", &year, &month);
if (month < 1 || month > 12) {
printf("输入错误!\n");
return 0;
}
flag = ifLeapYear(year);
if (flag) {
date = month1[month - 1];
} else {
date = month2[month - 1];
}
printf("%d年%d月有%d天\n", year, month, date);
return 0;
}
```