📜  检查给定年份是否为闰年的程序

📅  最后修改于: 2022-05-13 01:57:59.277000             🧑  作者: Mango

检查给定年份是否为闰年的程序

满足下列条件的年份为闰年:

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

以下是伪代码

if year is divisible by 400 then is_leap_year
else if year is divisible by 100 then not_leap_year
else if year is divisible by 4 then is_leap_year
else not_leap_year
C++
// C++ program to check if a given
// year is leap year or not
#include 
using namespace std;
 
bool checkYear(int year)
{
    // If a year is multiple of 400,
    // then it is a leap year
    if (year % 400 == 0)
        return true;
 
    // Else If a year is multiple of 100,
    // then it is not a leap year
    if (year % 100 == 0)
        return false;
 
    // Else If a year is multiple of 4,
    // then it is a leap year
    if (year % 4 == 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int year = 2000;
 
    checkYear(year) ? cout << "Leap Year":
                      cout << "Not a Leap Year";
    return 0;
}
 
// This is code is contributed
// by rathbhupendra


C
// C program to check if a given
// year is leap year or not
#include 
#include 
 
bool checkYear(int year)
{
    // If a year is multiple of 400,
    // then it is a leap year
    if (year % 400 == 0)
        return true;
 
    // Else If a year is multiple of 100,
    // then it is not a leap year
    if (year % 100 == 0)
        return false;
 
    // Else If a year is multiple of 4,
    // then it is a leap year
    if (year % 4 == 0)
        return true;
    return false;
}
 
// driver code
int main()
{
    int year = 2000;
 
    checkYear(year)? printf("Leap Year"):
                   printf("Not a Leap Year");
    return 0;
}


Java
// Java program to check
// for a leap year
     
class Test
{
    static boolean checkYear(int year)
    {
        // If a year is multiple of 400,
        // then it is a leap year
        if (year % 400 == 0)
            return true;
     
        // Else If a year is multiple of 100,
        // then it is not a leap year
        if (year % 100 == 0)
            return false;
     
        // Else If a year is multiple of 4,
        // then it is a leap year
        if (year % 4 == 0)
            return true;
        return false;
    }
         
    // Driver method
    public static void main(String[] args)
    {
        int year = 2000;
        System.out.println( checkYear(2000)? "Leap Year" :
                           "Not a Leap Year" );
    }
}


Python3
# Python program to check leap year or not
def checkYear(year):
    if (year % 4) == 0:
        if (year % 100) == 0:
            if (year % 400) == 0:
                return True
            else:
                return False
        else:
             return True
    else:
        return False
 
# Driver Code
year = 2000
if(checkYear(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")
     
# This code is contributed by Chinmoy Lenka


C#
// C# program to check
// for a leap year
using System;
 
class GFG
{
     
    static bool checkYear(int year)
    {
        // If a year is multiple of 400,
        // then it is a leap year
        if (year % 400 == 0)
            return true;
     
        // Else If a year is multiple of 100,
        // then it is not a leap year
        if (year % 100 == 0)
            return false;
     
        // Else If a year is multiple of 4,
        // then it is a leap year
        if (year % 4 == 0)
            return true;
        return false;
    }
         
    // Driver method
    public static void Main()
    {
        int year = 2000;
        Console.Write( checkYear(year)? "Leap Year" :
                                 "Not a Leap Year" );
    }
 
}
 
// This code is contributed by Sam007


PHP


Javascript


C++
// One line C program to check if a
// given year is leap year or not
#include 
using namespace std;
 
bool checkYear(int year)
{
     
    // Return true if year is a multiple
    // 0f 4 and not multiple of 100.
    // OR year is multiple of 400.
    return (((year % 4 == 0) && (year % 100 != 0)) ||
             (year % 400 == 0));
}
 
// Driver code
int main()
{
    int year = 2000;
 
    checkYear(year)? cout << "Leap Year":
                     cout << "Not a Leap Year";
    return 0;
}
 
// This code is contributed by Akanksha Rai


C
// One line C program to check if a
// given year is leap year or not
#include 
#include 
 
bool checkYear(int year)
{
// Return true if year is a multiple
// 0f 4 and not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0) && (year % 100 != 0)) ||
        (year % 400 == 0));
}
 
// driver code
int main()
{
    int year = 2000;
 
    checkYear(year)? printf("Leap Year"):
                printf("Not a Leap Year");
    return 0;
}


Java
// Java program to check
// for a leap year
     
class Test
{
    static boolean checkYear(int year)
    {
    // Return true if year is a multiple
    // of 4 and not multiple of 100.
    // OR year is multiple of 400.
    return (((year % 4 == 0) && (year % 100 != 0)) ||
            (year % 400 == 0));
    }
         
    // Driver method
    public static void main(String[] args)
    {
        int year = 2000;
        System.out.println(checkYear(2000)? "Leap Year" :
                           "Not a Leap Year" );
    }
}


Python3
# Python program to check leap year
# or not in a single line
 
def checkYear(year):
 
    # Return true if year is a multiple
    # of 4 and not multiple of 100.
    # OR year is multiple of 400.
    return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0));
 
# Driver Code
year = 2000
if(checkYear(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")
     
# This code is contributed by Chinmoy Lenka


C#
// C# program to check
// for a leap year
using System;
 
class GFG
{
    static bool checkYear(int year)
    {
        // Return true if year is a multiple
        // of 4 and not multiple of 100.
        // OR year is multiple of 400.
        return (((year % 4 == 0) && (year % 100 != 0)) ||
                (year % 400 == 0));
    }
         
    // Driver method
    public static void Main()
    {
        int year = 2000;
        Console.Write( checkYear(year)? "Leap Year" :
                                 "Not a Leap Year" );
    }
 
}
// This code is contributed by Sam007


PHP


Javascript


C++
// C++ implementation to check
// if the year is a leap year
// using macros
 
#include 
using namespace std;
 
// Macro to check if a year
// is a leap year
#define ISLP(y) ((y % 400 == 0) ||\
(y % 100 != 0) && (y % 4 == 0))
        
// Driver Code
int main()
{
    int year = 2020;
    cout << ISLP(year) << "\n";
    return 0;
}


Java
// Java implementation to check
// if the year is a leap year
// using macros
public class Main
{
  // Macro to check if a year
  // is a leap year
  static int ISLP(int y)
  {
    if((y % 400 == 0) ||
       (y % 100 != 0) &&
       (y % 4 == 0))
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int year = 2020;
    System.out.println(ISLP(year));
  }
}
 
// This code is contributed by divyeshrabadiya07.


Python3
# Python3 implementation to check
# if the year is a leap year
# using macros
 
# Macro to check if a year
# is a leap year
def ISLP(y):
  if((y % 400 == 0) or
     (y % 100 != 0) and
     (y % 4 == 0)):
    return 1;
  else:
    return 0;
 
# Driver code
if __name__=='__main__':
 
  year = 2020;
  print(ISLP(year));
 
  # This code is contributed by  Pratham76.


C#
// C# implementation to check
// if the year is a leap year
// using macros
using System;
class GFG {
     
  // Macro to check if a year
  // is a leap year
  static int ISLP(int y)
  {
    if((y % 400 == 0) ||
       (y % 100 != 0) &&
       (y % 4 == 0))
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
   
  // Driver code
  static void Main()
  {
    int year = 2020;
    Console.WriteLine(ISLP(year));
  }
}
 
// This code is contributed by divyesh072019


Javascript


Python
def checkYear(year):
    
    # Return true if year is a multiple
    # of 4 and not multiple of 100.
    # OR year is multiple of 400.
    import calendar
    return(calendar.isleap(year))
    
# Driver Code 
year = 2000
if (checkYear(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")
        
# This code is contributed by Chin


输出:

Leap Year

上面的代码怎么写成一行?

C++

// One line C program to check if a
// given year is leap year or not
#include 
using namespace std;
 
bool checkYear(int year)
{
     
    // Return true if year is a multiple
    // 0f 4 and not multiple of 100.
    // OR year is multiple of 400.
    return (((year % 4 == 0) && (year % 100 != 0)) ||
             (year % 400 == 0));
}
 
// Driver code
int main()
{
    int year = 2000;
 
    checkYear(year)? cout << "Leap Year":
                     cout << "Not a Leap Year";
    return 0;
}
 
// This code is contributed by Akanksha Rai

C

// One line C program to check if a
// given year is leap year or not
#include 
#include 
 
bool checkYear(int year)
{
// Return true if year is a multiple
// 0f 4 and not multiple of 100.
// OR year is multiple of 400.
return (((year % 4 == 0) && (year % 100 != 0)) ||
        (year % 400 == 0));
}
 
// driver code
int main()
{
    int year = 2000;
 
    checkYear(year)? printf("Leap Year"):
                printf("Not a Leap Year");
    return 0;
}

Java

// Java program to check
// for a leap year
     
class Test
{
    static boolean checkYear(int year)
    {
    // Return true if year is a multiple
    // of 4 and not multiple of 100.
    // OR year is multiple of 400.
    return (((year % 4 == 0) && (year % 100 != 0)) ||
            (year % 400 == 0));
    }
         
    // Driver method
    public static void main(String[] args)
    {
        int year = 2000;
        System.out.println(checkYear(2000)? "Leap Year" :
                           "Not a Leap Year" );
    }
}

Python3

# Python program to check leap year
# or not in a single line
 
def checkYear(year):
 
    # Return true if year is a multiple
    # of 4 and not multiple of 100.
    # OR year is multiple of 400.
    return (((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0));
 
# Driver Code
year = 2000
if(checkYear(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")
     
# This code is contributed by Chinmoy Lenka

C#

// C# program to check
// for a leap year
using System;
 
class GFG
{
    static bool checkYear(int year)
    {
        // Return true if year is a multiple
        // of 4 and not multiple of 100.
        // OR year is multiple of 400.
        return (((year % 4 == 0) && (year % 100 != 0)) ||
                (year % 400 == 0));
    }
         
    // Driver method
    public static void Main()
    {
        int year = 2000;
        Console.Write( checkYear(year)? "Leap Year" :
                                 "Not a Leap Year" );
    }
 
}
// This code is contributed by Sam007

PHP


Javascript


输出:

Leap Year

在 C/C++ 中使用宏检查闰年

如果年份是闰年,程序输出 1,如果不是闰年,程序输出 0。

C++

// C++ implementation to check
// if the year is a leap year
// using macros
 
#include 
using namespace std;
 
// Macro to check if a year
// is a leap year
#define ISLP(y) ((y % 400 == 0) ||\
(y % 100 != 0) && (y % 4 == 0))
        
// Driver Code
int main()
{
    int year = 2020;
    cout << ISLP(year) << "\n";
    return 0;
}

Java

// Java implementation to check
// if the year is a leap year
// using macros
public class Main
{
  // Macro to check if a year
  // is a leap year
  static int ISLP(int y)
  {
    if((y % 400 == 0) ||
       (y % 100 != 0) &&
       (y % 4 == 0))
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int year = 2020;
    System.out.println(ISLP(year));
  }
}
 
// This code is contributed by divyeshrabadiya07.

Python3

# Python3 implementation to check
# if the year is a leap year
# using macros
 
# Macro to check if a year
# is a leap year
def ISLP(y):
  if((y % 400 == 0) or
     (y % 100 != 0) and
     (y % 4 == 0)):
    return 1;
  else:
    return 0;
 
# Driver code
if __name__=='__main__':
 
  year = 2020;
  print(ISLP(year));
 
  # This code is contributed by  Pratham76.

C#

// C# implementation to check
// if the year is a leap year
// using macros
using System;
class GFG {
     
  // Macro to check if a year
  // is a leap year
  static int ISLP(int y)
  {
    if((y % 400 == 0) ||
       (y % 100 != 0) &&
       (y % 4 == 0))
    {
      return 1;
    }
    else
    {
      return 0;
    }
  }
   
  // Driver code
  static void Main()
  {
    int year = 2020;
    Console.WriteLine(ISLP(year));
  }
}
 
// This code is contributed by divyesh072019

Javascript


输出
1

Python中的简短解决方案:

Python

def checkYear(year):
    
    # Return true if year is a multiple
    # of 4 and not multiple of 100.
    # OR year is multiple of 400.
    import calendar
    return(calendar.isleap(year))
    
# Driver Code 
year = 2000
if (checkYear(year)):
    print("Leap Year")
else:
    print("Not a Leap Year")
        
# This code is contributed by Chin
输出
Leap Year