C Program to determine whether given year is leap year or not

      A year is leap year if that year is perfectly divisible by four – except for years which are both divisible by 100 and not divisible by 400. Most of us miss out the logic of divisibility test by 100 & 400. Make sure you don’t commit that mistake. The second part of the rule effects century years. The below programs checks whether a given year is leap year or not.
For example; the century years 1600 and 2000 are leap years,
but the century years 1700, 1800, and 1900 are not. This means that three times out of every four hundred years
there are eight years between leap years.

leap year or not program in c language



Source Code to find leap year or not:

#include <stdio.h>
int main()
{
	int year, rem_4,rem_100,rem_400;
	printf("Enter the year to be tested:");
	scanf("%d", &year);                                         //Step 1 
	rem_4 = year % 4;                                           //Step 2 
	rem_100 = year % 100;                                       //Step 3   
	rem_400 = year % 400;                                       //Step 3
	if( (rem_4 == 0 && rem_100 != 0) || rem_400 == 0)           //Step 4
		printf("It is a leap year.\n");
	else
		printf("It is not a leap year.\n");
	return 0;
}


edit-code-image

Sample Test cases:

1. Enter the year to be tested: 1800
   It is not a leap year
   
2. Enter the year to be tested: 1600
   It is a leap year
   
3. Enter the year to be tested: 2017
   It is not a leap year

Explanation:

Step 1: Program reads an year from the user using scanf() function to find whether it is leap year or not.

Step 2: The remainder of a given number when divided by 4 is stored in variable ‘rem_4’.

Step 3: Similarly the remainder when divided by 100 and 400 are stored in ‘rem_100’ and ‘rem_400’ respectively.

Step 4: Using If conditional statement the condition for leap year is tested and the result is printed to output screen.

More Insights:

1. Leap Year Rules.

2. Why 1800 is leap year eventhough it is divisible by 4.

Explore C Programs:C Programs