用C语言输入年份及月份知道该月天数
时间: 2023-05-27 18:07:40 浏览: 94
以下是用C语言编写的程序,输入年份和月份,输出该月的天数:
```c
#include <stdio.h>
int main()
{
int year, month, days;
printf("请输入年份和月份:\n");
scanf("%d %d", &year, &month);
if (month == 2) // 二月特殊处理
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
days = 29;
}
else
{
days = 28;
}
}
else if (month == 4 || month == 6 || month == 9 || month == 11) // 四月、六月、九月、十一月
{
days = 30;
}
else // 其他月份
{
days = 31;
}
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
```
程序首先读入年份和月份,然后根据月份来计算天数。对于二月份需要特殊处理,闰年有29天,平年有28天。其他月份根据月份判断天数即可。最后输出结果。
相关问题
用c语言输入年份和月份,输出该年该月的天数
```c
#include <stdio.h>
int main() {
int year, month, days;
printf("请输入年份和月份:\n");
scanf("%d%d", &year, &month);
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
```
说明:
* 如果是2月份,需要判断是否为闰年,闰年2月份有29天,平年2月份有28天。
* 如果是4、6、9、11月份,每月有30天。
* 其他月份每月有31天。
c语言根据输入月份和年份求该月天数
以下是一个示例程序,可以根据输入的月份和年份计算该月的天数:
```c
#include <stdio.h>
int main()
{
int year, month, days;
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &month);
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:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
default:
printf("输入的月份有误!\n");
return 1;
}
printf("%d年%d月有%d天。\n", year, month, days);
return 0;
}
```
程序中使用了一个 switch 语句来根据月份计算天数。其中,1、3、5、7、8、10、12 月都有 31 天,4、6、9、11 月都有 30 天,2 月根据年份是否为闰年来确定天数。如果是闰年,则有 29 天;否则只有 28 天。注意,闰年的判断方法是:能被 4 整除但不能被 100 整除,或者能被 400 整除。