c语言输出“从键盘输入某年某月(包括闰年),编程输出该年的该月拥有的天数。 **输入格式要求:"%d,%d" 提示信息:"Input year,month:" "The number of days is %d\n" **输出格式要求:"%d" 程序运行示例1如下: Input year,month:1984,2 The number of days is 29 程序运行示例2如下: Input year,month:2000,2 The number of days is 29 程序运行示例3如下: Input year,month:1985,2 The number of days is 28 程序运行示例4如下: Input year,month:1983,13 Input year,month:1983,-1 Input year,month:1983,1 The number of days is 31
时间: 2024-03-03 17:52:00 浏览: 74
好的,以下是 C 语言的代码实现:
```
#include <stdio.h>
int main() {
int year, month, days;
printf("Input year,month:");
scanf("%d,%d", &year, &month);
if (month < 1 || month > 12) { // 判断月份是否合法
printf("Input error\n");
return 0;
}
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) { // 判断30天的月份
days = 30;
} else { // 判断31天的月份
days = 31;
}
printf("The number of days is %d\n", days);
return 0;
}
```
注意事项:
1. 输入格式为"%d,%d",不要输错了,否则程序会出现异常。
2. 月份的输入应该在 1~12 之间,否则程序会提示输入错误。
3. 在计算二月份天数时,需要判断是否为闰年来确定天数。
4. 最后输出天数时,注意要使用格式化输出,输出格式为"%d"。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)