编写一个函数判断是否为输入数字为闰年,如果是输出yes
时间: 2024-03-03 17:50:32 浏览: 22
好的,以下是一个 Python 编写的判断闰年的函数:
```python
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return "yes"
else:
return "no"
```
你可以传入一个年份作为参数,函数将返回字符串 "yes" 或 "no",表示该年份是否为闰年。比如:
```python
>>> is_leap_year(2020)
'yes'
>>> is_leap_year(2021)
'no'
```
希望能够帮到你!
相关问题
用c语言编写一个函数判断是否为输入数字为闰年,如果是输出yes反之输出no
好的,以下是一个 C 语言编写的判断闰年的函数:
```c
#include <stdio.h>
int is_leap_year(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return 1;
} else {
return 0;
}
}
int main() {
int year;
printf("请输入一个年份:");
scanf("%d", &year);
if (is_leap_year(year)) {
printf("yes\n");
} else {
printf("no\n");
}
return 0;
}
```
你可以调用 `is_leap_year` 函数,传入一个年份作为参数,该函数将返回 1 或 0,表示该年份是否为闰年。在 `main` 函数中,我们通过调用 `is_leap_year` 函数来判断输入的年份是否为闰年,如果是则输出 "yes",否则输出 "no"。
希望能够帮到你!
编写函数 int fun(int n)判断n是否是闰年,定义main函数输入年份,调用fun函数判断,在main函数中输出yes或no
好的,可以使用以下代码实现:
```C++
#include <iostream>
using namespace std;
int fun(int n) {
if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0) {
return 1; // 是闰年
} else {
return 0; // 不是闰年
}
}
int main() {
int year;
cout << "请输入年份:";
cin >> year;
if (fun(year)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
return 0;
}
```
在函数`fun`中,如果年份能够被4整除且不能被100整除,或者能够被400整除,则认为是闰年,返回1;否则返回0。
在主函数中,先输入年份,再调用`fun`函数判断,如果是闰年,输出"yes",否则输出"no"。