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

闰年是有 366 天的一年。闰年增加一天,以保持日历年与天文年同步。您可以使用数学和编程来检查给定年份是否为闰年。

在本文中,您将学习如何使用 C++、Python、JavaScript 和 C 来检查给定年份是否为闰年。

问题陈述

给你一年。您需要检查给定的年份是否为闰年。

示例 1 :让年份 = 2021。

2021年不是闰年。

因此,输出为“2021 年不是闰年”。

示例 2 :让年份 = 1980。

1980年是闰年。

因此,输出为“1980 年是闰年”。

一年为闰年的条件

满足下列条件之一或两者的年份为闰年:

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

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

以下是用于检查给定年份是否为闰年的 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、C++ 和 JavaScript 中打印一个?

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

以下是检查给定年份是否为闰年的 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 程序:

 // 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 程序:

 // 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

使用编码应用程序使编程更容易

如果您正在寻找一种有趣的方式来学习编码,您可以使用一些应用程序,如 Enki、Grasshopper、SoloLearn、Codeacademy Go、Hopscotch、Encode 等。这些应用程序提供了以有趣和互动的方式学习编码的绝佳环境大大地。无论您身在何处,他们都会通过编码帮助您保持领先地位。