要求定义和调用函数month_days,该函数返回year年month月的天数
时间: 2023-04-03 20:03:46 浏览: 120
好的,我可以回答这个问题。下面是函数month_days的定义和调用方法:
函数定义:
int month_days(int year, int month){
int days;
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;
}
return days;
}
函数调用:
int days = month_days(2021, 8);
printf("2021年8月的天数是:%d", days);
输出结果为:2021年8月的天数是:31
希望能够帮助您解决问题。
相关问题
函数month_days的定义和调用,求该函数返回year年month月的天数
函数month_days的定义和调用如下:
定义:
int month_days(int year, int month) {
int days;
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;
}
return days;
}
调用:
int days = month_days(2022, 8);
该函数返回2022年8月的天数,即31天。
使用c语言,输入年year,输出该年1到12月每个月的天数。定义并调用month_days,该函数返回year年month月的天数
以下是使用C语言编写的实现代码:
```c
#include <stdio.h>
int month_days(int year, int month) {
int days;
switch (month) {
case 2:
days = 28 + ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
default:
days = 31;
break;
}
return days;
}
int main() {
int year, month;
printf("请输入年份:");
scanf("%d", &year);
for (month = 1; month <= 12; month++) {
printf("%d年%d月有%d天\n", year, month, month_days(year, month));
}
return 0;
}
```
函数`month_days`根据年份和月份计算该月的天数,使用了`switch`语句和条件运算符。在主函数中,使用循环输出每个月的天数。运行结果如下:
```
请输入年份:2022
2022年1月有31天
2022年2月有28天
2022年3月有31天
2022年4月有30天
2022年5月有31天
2022年6月有30天
2022年7月有31天
2022年8月有31天
2022年9月有30天
2022年10月有31天
2022年11月有30天
2022年12月有31天
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)