logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jun02/leapYear.c

/*

	This program takes an input representing the year and then
	determines if this is a leap year.

	Here in the US, we use the Gregorian Calendar, so the Leap Year Rules are as follows

	If a year is divisible by 4, it is a leap year, execpt when it is divisible by 100.
	In that case, that year is not a leap year unless it is also divisible by 400. 

*/


#include <stdio.h>

int main()
{
	int year; //declare a variable for year

	printf("Enter a year: ");//Prompt the user for a year
	scanf("%d", &year); //store the user input in the year variable

	/*
		then below, use if-else statements and the modulus operator to calculate if the given year is a leap year
		and then output to the user whether or not the given year is a leap year
		
	    we do the divisible by 100 case first because while all numbers divisible by 100 are also divisible by 4, the rules
	    make it so that years that end in 00 are NOT leap years unless they are divisible by 400. given the fact that in C 
	    programming, we start at the very top and work our way down, we need to deal with that case first, so we don't end up
	    with an incorrect output
	    
	*/


	/*
		using the modulus operator

		a % b = c, where c is the remainder from when you do a / b
		if c equals 0, then b will divide evenly into a.
	*/


	if(year % 100 == 0){// checking if the year is divisible by 100, special case
		if(year % 400 == 0) //is year divisible by 400?
			printf("%d is a leap year\n", year);
		else
			printf("%d is NOT a leap year\n", year);

	} else { //year not ending in 00, proceed as normal.
		if(year % 4 == 0)
			printf("%d is a leap year\n", year);
		else
			printf("%d is NOT a leap year\n", year);
	}



	return 0;
}