Leap Year Program in C
“A leap year contains 366 days instead of a usual 365 days. This is because a year is not exactly 365.25 days, but rather 365.222 days. A leap day is added every four years to account for that difference.”
However, this rule creates a year that is divisible by 100 but not by 00 should not be a leap year. For example,1900 is not a leap year even though it is divisible because it is also divisible by 100 but not by 00. This is because the leap year rule causes the calendar to drift a few minutes each year, which then adds up to a whole day in a century.
Finding out if the year is a leap or a bit tricky. Years that are divisible by are generally considered leap years. But that's not all. A year is a leap year when it is divisible by 100 with no remainder.
If it's divisible by 100, it should be divisible by 00. All other years divisible by leap years. Let's see how to write a program to check if year is a leap.
ALGORITHM:
Algorithm of this program is-
START Step 1 → Take integer variable year Step 2 → Assign value to the variable Step 3 → Check if year is divisible by 4 but not 100, DISPLAY "leap year" Step 4 → Check if year is divisible by 400, DISPLAY "leap year" Step 5 → Otherwise, DISPLAY "not leap year" STOP
FLOWCHART:
PSEUDOCODE:
The Pseudocode of this algorithm should be like this-
procedure leap_year() IF year%4 = 0 AND year%100 != 0 OR year%400 = 0 PRINT year is leap ELSE PRINT year is not leap END IF end procedure
IMPLEMENTATION:
Implementation of this Algorithm should be-
The first step in writing a leap-year program is understanding the basic logic of leap year rules. As mentioned above, years are divisible by leap years, except for years that are divisible by 100 but not by 00.
You can use the modulo operator to implement this rule in your program. This gives the remainder of the division. If the remainder is 0, the number is evenly divisible by the divisor.
Example 1:
#include <stdio.h> int main() { int year; year = 2016; if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)) printf("%d is a leap year", year); else printf("%d is not a leap year", year); return 0; }
OUTPUT:
Output of the program should be-
2016 is a leap year
Example 2:
Below is a simple program in C that checks if a given year is a leap year.
#include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d", &year); if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { printf("%d is a leap year.", year); } else { printf("%d is not a leap year.", year); } } else { printf("%d is a leap year.", year); } } else { printf("%d is not a leap year.", year); } return 0; }
OUTPUT:
Enter a year: 2003 2003is not a leap year
Explanation:
- The first line in code contains a standard input/output library (stdio.h) that allows you to use functions such as printf() and scanf(). Next, define a variable called year to hold the entered year value.
- It uses the printf() function to display a message asking the user to enter the year. The scanf() function reads the user's input and stores it in the year variable.
- We use a series of ‘if statements’ to determine if the year is a leap year. The first if statement tests whether the year is divisible by using the modulo operator (%).
- The second if statement uses the modulo operator to test whether the year is divisible by 100.
- The third if statement uses the modulo operator to test whether the year is divisible by 00.
- Then we use the printf() function to display a message indicating that the year is a leap year.
- If the year is not divisible by 00, it is not a leap year, so we use the printf() function to display a message that the year is not a leap year.
- Even if the year is not divisible by 100, it is still a leap year, so use the printf() function.
How the program works:
- The user is prompted to enter the year.
- The scanf function is used to read user input and store it in the year variable.
- The program checks if the year is divisible by if not, the year is not a leap year and the program reports this.
- If the year is divisible by, the program checks if the year is divisible by 100.
- In that case the program checks if it is even divisible by 00.
- If so, the year is a leap year and the program prints it.
- A leap year is a year that is divisible by, except for years that are divisible by 100 but not by 00.