编程输入年份和月份,输出这一年的这个月份有多少天。c语言
时间: 2024-05-05 15:17:15 浏览: 24
实现如下:
```
#include <stdio.h>
int main() {
int year, month, days;
printf("请输入年份和月份(用空格隔开):");
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;
}
```
首先,我们使用 `scanf()` 函数从用户输入中读取年份和月份。然后,使用条件语句来判断这个月有多少天。对于二月份,需要判断是否为闰年,如果是,则有 29 天,否则有 28 天。对于其它月份,根据月份的奇偶性来判断天数,其中 4、6、9、11 月份有 30 天,其余月份有 31 天。最后,使用 `printf()` 函数输出结果。
相关问题
编程输入年份和月份,输出这一年的这个月份有多少天。C语言
实现如下:
```c
#include <stdio.h>
int main()
{
int year, month, days;
printf("请输入年份和月份(例如:2020 8):");
scanf("%d %d", &year, &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 0;
}
printf("%d年%d月有%d天\n", year, month, days);
return 0;
}
```
首先通过 `scanf` 函数读入年份和月份。然后使用 `switch` 语句根据月份计算这个月有多少天。需要注意的是,闰年的二月份有 29 天,需要特判。最后输出结果。
用c语言编程输入年份和月份,输出这一年的这个月份有多少天 输入样例:2015 10 输出样例:31
```c
#include <stdio.h>
int main()
{
int year, month, days;
printf("请输入年份和月份:");
scanf("%d%d", &year, &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 0;
}
printf("%d年%d月有%d天。\n", year, month, days);
return 0;
}
```