Home C# How to check if a year is a leap year in C

How to check if a year is a leap year in C

In this example we show you how to check if a year is a leap year in C

To determine whether a year is a leap year, follow these steps:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

Example

#include <stdio.h>

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

   // is leap year if divisible by 400
   if (year % 400 == 0) 
   {
      printf("%d is a leap year.", year);
   }
   // not leap year if divisible by 100 but not by 400
   else if (year % 100 == 0) 
   {
      printf("%d is not a leap year.", year);
   }
   // is leap year if not divisible by 100 but by 4
   else if (year % 4 == 0) 
   {
      printf("%d is a leap year.", year);
   }
   // not leap years
   else 
   {
      printf("%d is not a leap year.", year);
   }

   return 0;
}

 

Here are a few test runs

Enter a year: 2000
2000 is a leap year.

Enter a year: 2002
2002 is not a leap year.

Enter a year: 2004
2004 is a leap year.

You may also like