C program for leap year

(Last Updated On: February 26, 2019)

C program for leap year – Example with logic, description and C syntax expression to find a leap year.

 

Simple logic to find Leap year?

  1. Find if the year is perfectly divisible by 4. If not divisible, then not a leap year.
  2. If divisible by 4 then, it can be a leap year. So, now, check if it is perfectly divisible by 100.
  3. If not divisible by 100 then it is a leap year, but, If it is divisible by 100 then it can be a leap year, So, now, check if the year is divisible by 400, if divisible then it is a leap year or else not a leap year.

 

Here are some years that can be checked with above logic whether they are leap year or not.

NOT A LEAP YEARS:1700, 1800, 1900, 1971 and 2006
LEAP YEARS :1200, 1600, 2000, 2400 ,2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

 

The same years can be tested by executing the C program given below.

 

Program description:
This C program prompt user to enter a year and checks if that year is leap year or not using function int isLeapYear(int year) written in the program below . Output will be displayed on the console screen if the year is a leap year or not a leap year. Also, code comments has been provided for better understanding.

Example of C program for leap year

 

Output:

To check the output you can run the program and enter the following years to test whether they are leap year or not.

 

Below years will output as the year is not a leap year
1700, 1800, 1900, 1971 and 2006

 

Below years will output as the year is a leap year
1200, 1600, 2000, 2400 ,2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

 

NOTE: In the function if else conditional expression can be also written as one liner expression in the program. Here is the code example.