# 如何用多种语言检查一年是否是闰年

2021年不是闰年。

1980年是闰年。

## 一年为闰年的条件

1. 年份是 400 的倍数。
2. 年份是 4 的倍数，而不是 100 的倍数。

## C++程序来检查一年是否是闰年

` `// C++ program to check if a given year is a leap year or not #include <iostream> using namespace std; bool isLeapYear(int y) { // If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400, // then the year is a leap year // Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)); } int main() { int year1 = 2021; cout << "Year: " << year1 << endl; if(isLeapYear(year1)) { cout << year1 << " is a leap year" << endl; } else { cout << year1 << " is not a leap year" << endl; } int year2 = 2012; cout << "Year: " << year2 << endl; if(isLeapYear(year2)) { cout << year2 << " is a leap year" << endl; } else { cout << year2 << " is not a leap year" << endl; } int year3 = 1980; cout << "Year: " << year3 << endl; if(isLeapYear(year3)) { cout << year3 << " is a leap year" << endl; } else { cout << year3 << " is not a leap year" << endl; } int year4 = 1990; cout << "Year: " << year4 << endl; if(isLeapYear(year4)) { cout << year4 << " is a leap year" << endl; } else { cout << year4 << " is not a leap year" << endl; } int year5 = 1600; cout << "Year: " << year5 << endl; if(isLeapYear(year5)) { cout << year5 << " is a leap year" << endl; } else { cout << year5 << " is not a leap year" << endl; } return 0; }``

` `Year: 2021 2021 is not a leap year Year: 2012 2012 is a leap year Year: 1980 1980 is a leap year Year: 1990 1990 is not a leap year Year: 1600 1600 is a leap year``

## Python程序来检查一年是否是闰年

` `# Python program to check if a given year is a leap year or not def isLeapYear(y): # If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400, # then the year is a leap year # Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year return (((y % 4 == 0) and (y % 100 != 0)) or (y % 400 == 0)) year1 = 2021 print("Year: ", year1) if isLeapYear(year1): print(year1, "is a leap year") else: print(year1, "is not a leap year") year2 = 2012 print("Year: ", year2) if isLeapYear(year2): print(year2, "is a leap year") else: print(year2, "is not a leap year") year3 = 1980 print("Year: ", year3) if isLeapYear(year3): print(year3, "is a leap year") else: print(year3, "is not a leap year") year4 = 1990 print("Year: ", year4) if isLeapYear(year4): print(year4, "is a leap year") else: print(year4, "is not a leap year") year5 = 1600 print("Year: ", year5) if isLeapYear(year5): print(year5, "is a leap year") else: print(year5, "is not a leap year")``

## JavaScript 程序来检查一年是否是闰年

` `// JavaScript program to check if a given year is a leap year or not function isLeapYear(y) { // If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400, // then the year is a leap year // Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)); } var year1 = 2021; document.write("Year: " + year1 + "<br>"); if(isLeapYear(year1)) { document.write(year1 + " is a leap year" + "<br>"); } else { document.write(year1 + " is not a leap year" + "<br>"); } var year2 = 2012; document.write("Year: " + year2 + "<br>"); if(isLeapYear(year2)) { document.write(year2 + " is a leap year" + "<br>"); } else { document.write(year2 + " is not a leap year" + "<br>"); } var year3 = 1980; document.write("Year: " + year3 + "<br>"); if(isLeapYear(year3)) { document.write(year3 + " is a leap year" + "<br>"); } else { document.write(year3 + " is not a leap year" + "<br>"); } var year4 = 1990; document.write("Year: " + year4 + "<br>"); if(isLeapYear(year4)) { document.write(year4 + " is a leap year" + "<br>"); } else { document.write(year4 + " is not a leap year" + "<br>"); } var year5 = 1600; document.write("Year: " + year5 + "<br>"); if(isLeapYear(year5)) { document.write(year5 + " is a leap year" + "<br>"); } else { document.write(year5 + " is not a leap year" + "<br>"); }``

` `Year: 2021 2021 is not a leap year Year: 2012 2012 is a leap year Year: 1980 1980 is a leap year Year: 1990 1990 is not a leap year Year: 1600 1600 is a leap year``

## C程序检查一年是否为闰年

` `// C program to check if a given year is a leap year or not #include <stdio.h> #include <stdbool.h> bool isLeapYear(int y) { // If a year is a multiple of 4 and not a multiple of 100 or a multiple of 400, // then the year is a leap year // Multiple of 4 and (Not a multiple of 100 or Multiple of 400) => A Leap year return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)); } int main() { int year1 = 2021; printf("Year: %d ⁠n", year1); if(isLeapYear(year1)) { printf("%d is a leap year ⁠n", year1); } else { printf("%d is not a leap year ⁠n", year1); } int year2 = 2012; printf("Year: %d ⁠n", year2); if(isLeapYear(year2)) { printf("%d is a leap year ⁠n", year2); } else { printf("%d is not a leap year ⁠n", year2); } int year3 = 1980; printf("Year: %d ⁠n", year3); if(isLeapYear(year3)) { printf("%d is a leap year ⁠n", year3); } else { printf("%d is not a leap year ⁠n", year3); } int year4 = 1990; printf("Year: %d ⁠n", year4); if(isLeapYear(year4)) { printf("%d is a leap year ⁠n", year4); } else { printf("%d is not a leap year ⁠n", year4); } int year5 = 1600; printf("Year: %d ⁠n", year5); if(isLeapYear(year5)) { printf("%d is a leap year ⁠n", year5); } else { printf("%d is not a leap year ⁠n", year5); } return 0; }``

` `Year: 2021 2021 is not a leap year Year: 2012 2012 is a leap year Year: 1980 1980 is a leap year Year: 1990 1990 is not a leap year Year: 1600 1600 is a leap year``