C Program to print Leap Years between two given years!

In this post, we are going to write a C program to print leap years between two given years.

So basically we need to collect the start year and end year from the user, then we need to loop between the start and end year and then find the leap years to print.

But before if you want to know the formula to find the leap year here it is,

1. A year should be evenly divisible by 4 (should not have a reminder):

    year % 4 == 0 ( reminder should be 0 )

2. AND Year should not be evenly divisible by 100 (should have a reminder):

    year % 100 != 0 ( reminder should not be 0 )

3. OR Year should be evenly divisible by 400 (should have a reminder):

    year%400 == 0 ( reminder should be 0 )

So basically 1 && 2 || 3

if (1  and 2) both are true or 3 is true then it is a leap year

Here is the entire script for you,

#include <stdio.h>
int main()
{
	int startYear, endYear, i;
	
	//print a string about the program
	printf("Print leap years between two given years \n");
    
    //get the start year from user
	printf("Enter Start year: ");
   	scanf("%d", &startYear);
   	
    //get the end year from user
	printf("Enter End year: ");
   	scanf("%d", &endYear);
	
	printf("Leap years:\n");	
	
	//loop through between the start and end year
	for(i=startYear; i <= endYear; i++){
		
		//check if the (i) year is a Leap year if yes print
		if( (0 == i % 4) && (0 != i % 100) || (0 == i % 400) ){
			printf("%d\n", i);
		}
	}
	
    return 0;
}

Go through the comments in the above program to understand it.

Example output of the above,

Print leap years between two given years
Enter Start year: 1990
Enter End year: 2020
Leap years:
1992
1996
2000
2004
2008
2012
2016
2020

--------------------------------
Process exited after 2.838 seconds with return value 0
Press any key to continue . . .

Enjoy coding.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top