自定义函数fun(int year)调用该函数,判断年份是否为闰年
时间: 2023-09-27 21:06:15 浏览: 209
好的,以下是判断闰年的方法:
```
int fun(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 1; // 是闰年,返回1
} else {
return 0; // 不是闰年,返回0
}
}
```
这个函数的参数是一个年份,返回值为1表示该年是闰年,返回值为0表示该年不是闰年。
相关问题
用include<stdio.h写自定义有函数fun(int year),主函数调用该函数,判断某年份是否为闰年。
好的,以下是使用 include<stdio.h> 的代码实现:
```c
#include <stdio.h>
#include <stdbool.h>
bool fun(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
else {
return false;
}
}
int main() {
int year;
printf("请输入年份:");
scanf("%d", &year);
if (fun(year)) {
printf("%d年是闰年!\n", year);
}
else {
printf("%d年不是闰年!\n", year);
}
return 0;
}
```
与之前的代码实现相似,函数 `fun(int year)` 判断该年份是否为闰年,并返回一个布尔值。在主函数中,我们使用 `scanf` 输入一个年份,并调用 `fun` 函数来判断该年份是否为闰年。如果是闰年,则使用 `printf` 输出该年份是闰年,否则输出该年份不是闰年。
文件题 (分值50.0分,难度:易) 自定义有函数fun(int year),主函数调用该函数,判断某年份是否为闰年。
好的,这是一个简单的判断闰年的问题。以下是代码实现:
```c++
#include <iostream>
using namespace std;
bool fun(int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return true;
}
else {
return false;
}
}
int main() {
int year;
cout << "请输入年份:";
cin >> year;
if (fun(year)) {
cout << year << "年是闰年!" << endl;
}
else {
cout << year << "年不是闰年!" << endl;
}
return 0;
}
```
函数 `fun(int year)` 中,我们采用了闰年的判断规则:如果该年份能被4整除但不能被100整除,或者能被400整除,则该年份为闰年。函数返回一个布尔值,表示该年份是否为闰年。
在主函数中,我们通过 `cin` 输入一个年份,并调用 `fun` 函数来判断该年份是否为闰年。如果是闰年,则输出该年份是闰年,否则输出该年份不是闰年。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)