判断闰年的规则如下:
1. 如果年份能被400整除,则是闰年。
2. 如果年份能被100整除但不能被400整除,则不是闰年。
3. 如果年份能被4整除但不能被100整除,则是闰年。
根据这些规则,可以编写一个简单的程序来判断任意年份是否为闰年。以下是几种不同编程语言的实现示例:
Python 示例
```python
def is_leap_year(year):
if year % 400 == 0:
return True
if year % 100 == 0:
return False
if year % 4 == 0:
return True
return False
year = int(input("请输入年份: "))
if is_leap_year(year):
print(f"{year} 是闰年")
else:
print(f"{year} 是平年")
```
Java 示例
```java
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入年份: ");
int year = in.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year + " 是闰年");
} else {
System.out.println(year + " 是平年");
}
}
}
```
C 语言示例
```c
include
int main() {
int year;
printf("请输入年份: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d 是闰年\n", year);
} else {
printf("%d 是平年\n", year);
}
return 0;
}
```
JavaScript 示例
```javascript
function isLeapYear(year) {
if (year % 400 === 0) {
return true;
}
if (year % 100 === 0) {
return false;
}
if (year % 4 === 0) {
return true;
}
return false;
}
const year = parseInt(prompt("请输入年份:"));
if (isLeapYear(year)) {
console.log(`${year} 是闰年`);
} else {
console.log(`${year} 是平年`);
}
```
这些示例代码都遵循了闰年的判断规则,并根据用户输入的年份输出是否为闰年。你可以选择任意一种编程语言来实现这个功能。