Learn how to write a C program to determine whether a given year is a leap year or not. This guide explains the rules for identifying leap years (divisible by 4 but not by 100, unless also divisible by 400) and how to implement these rules in code. The tutorial includes step-by-step instructions, sample code, and explanations to help you accurately perform this check in C.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Inside the main() function:
- An integer variable `year` is declared to store the year entered by the user.
- The user is prompted to enter a year.
- Using conditional statements, it checks whether the year is divisible by 4 but not divisible by 100, or divisible by 400.
- If the condition is true, it prints that the year is a leap year; otherwise, it prints that the year is not a leap year.