Program to check if a given year is leap year

1.5

A year is leap year if following conditions are satisfied
1) Year is multiple of 400
2) Year is multiple of 4 and not multiple of 100.

Following is pseudo code

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 <stdio.h>
#include <stdbool.h>

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 muliplt of 100, then it is not a
    // leap year
    if (year%100 == 0)
        return false;

    // Else If a year is muliplt of 4, then it is a leap year
    if (year%4 == 0)
        return true;
    return false;
}


int main()
{
    int year;
    printf("Enter a year to be checkedn");
    scanf("%d", &year);

    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 muliplt of 100, then it is not a
        // leap year
        if (year%100 == 0)
            return false;
     
        // Else If a year is muliplt 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" );
    }
}


Output:
Enter a year to be checked
2000
Leap Year

 

How to write above code in one line?

C

// One line C program to check if a given
// year is leap year or not
#include <stdio.h>
#include <stdbool.h>

bool checkYear(int year)
{
  // Return true if year is a multiple pf 4 and
  // not multiple of 100.
  // OR year is multiple of 400.
  return (((year%4==0) && (year%100!=0)) ||
           (year%400==0));
}


int main()
{
    int year;
    printf("Enter a year to be checkedn");
    scanf("%d", &year);

    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 pf 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" );
    }
}


Output:
Enter a year to be checked
2000
Leap Year

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.5 Average Difficulty : 1.5/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.