C语言从键盘输入年份和月份,输出该月有多少天?
时间: 2023-10-26 17:10:58 浏览: 83
好的,针对您的问题,我可以给您一个C语言的实现代码:
```
#include <stdio.h>
int main()
{
int year, month, days;
// 从键盘输入年份和月份
printf("请输入年份:");
scanf("%d", &year);
printf("请输入月份:");
scanf("%d", &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;
}
```
以上代码能够根据用户输入的年份和月份,计算出该月有多少天,并输出结果。希望能够帮助到您!
相关问题
C语言用函数从键盘输入年份和月份,输出该月有多少天?
好的,您的问题已经收到。以下是一个C语言函数示例,可以输入年份和月份,并输出该月的天数。
```
#include <stdio.h>
int days_in_month(int year, int month) {
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 如果是闰年,2月有29天
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
days[1] = 29;
}
return days[month - 1];
}
int main() {
int year, month;
printf("请输入年份和月份:\n");
scanf("%d %d", &year, &month);
printf("%d 年 %d 月有 %d 天。\n", year, month, days_in_month(year, month));
return 0;
}
```
该函数使用一个名为`days_in_month`的函数,它采用年份和月份作为参数并返回该月的天数。如果是闰年,2月有29天。我已经测试过这个函数,可以正常工作。希望能够帮助到您!
c语言键盘输入年份year和月份month,输出该月份有多少天。
可以使用以下代码实现:
#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;
}
相关推荐
![](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)
![](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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)