编写一个程序,输出给定的某年某月的天数。提示:用switch语句对月份进行判断,分别进行处理,但注意二月有闰年和非闰年之分,可在case语句里嵌套if-else。判断某年是闰年,即这年能被4整除但不能被100整除,或这年能被400整除。
时间: 2023-05-31 22:03:33 浏览: 84
#include <stdio.h>
int main()
{
int year, month, days;
printf("请输入年份和月份:\n");
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;
}
相关问题
用c语言编写一个程序,输出给定的某年某月的天数
你好,我可以回答这个问题。以下是用 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;
}
```
以上代码中,我们使用了 switch 语句来根据输入的月份计算该月的天数。对于闰年的判断,我们使用了 if 语句和逻辑运算符。最后,我们使用 printf 函数输出结果。
希望能够帮助到你!
编写一个程序,输出给定的某年某月的天数。
```
#include <stdio.h>
int main() {
int year, month, days;
printf("请输入年份和月份:\n");
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` 语句判断该月份的天数,其中,对于 1、3、5、7、8、10、12 月份,天数为 31 天;对于 4、6、9、11 月份,天数为 30 天;对于 2 月份,需要判断是否是闰年,如果是闰年,天数为 29 天,否则为 28 天。
- 如果用户输入的月份不在 1-12 的范围内,则输出错误提示。