C Program to check Leap Year (2 ways)

By | August 27, 2020

This program checks whether the input year is leap year or not.

A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

Leap Year :

  • If a year is divisible by 4, 100 and 400 then it is a leap year.
  • If a year is divisible by 4 but not divisible by 100 then it is a leap year

Not a Leap Year :

  • If a year is not divisible by 4 then it is not a leap year.
  • If a year is divisible by 4 and 100 but not divisible by 400 then it is not a leap year.

Algorithm :

  1. Take a year as input.
  2. Check whether a given year is divisible by 400.
  3. Check whether a given year is divisible by 100.
  4. Check whether a given year is divisible by 4.
  5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
  6. If the condition at step 3 becomes true, then the year is not a leap year.

Implementation :

1. Using function :

// C Program to check Leap Year
#include <stdio.h> 

// function to check condtiond  
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; 
} 
  
// driver code 
int main() 
{ 
    // take input
    int year = 2020; 
  
    checkYear(year)? printf("Leap Year"): 
                   printf("Not a Leap Year"); 
                   
    // successful completion
    return 0; 
} 

Output :

Leap Year 

2. Check in one line :

// C Program to check Leap Year
#include <stdio.h>

int main() {
    
    // take input
   int year =2020;

   // check condtions
   if (((year % 4 == 0) && (year % 100 != 0)) || (year%400 == 0))
      printf("Leap Year", year);
   else
      printf("Not a Leap Year", year);
   
   // successful completion
   return 0;
}

Output :

Leap Year 



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.