In C programming, checking if a year is a leap year is pretty common. Like, the year 2024 is a leap year, but 2100 isn't. This program helps figure out if a specific year meets those leap year rules.
You’ll learn the code and the logic behind checking leap years, which means doing some divisibility tests. Let’s dive into what you’ll need and how to code it.
Prerequisites for the Leap Year Program in C
Before you start coding, ensure you have the following:
- Understanding of conditional statements like
ifandelse - Use of the modulus
%operator - Using
<stdio.h>library for input and output - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Check Leap Year
Here's the complete source code to check if a year is a leap year:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Check if year is a leap 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;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Leap Year Program
Here's what you should see when you run this program:
Enter a year: 2024
2024 is a leap year.

Algorithm to Check Leap Year in C
Checking if a year is a leap year involves specific divisibility tests. Here's how the algorithm processes it step by step.
- Read the year from the user.
- Check if the year is divisible by 4.
- If divisible by 4, check if it's also divisible by 100.
- If divisible by 100, check if it's divisible by 400.
- If any of the above conditions match, declare it a leap year. Otherwise, it's not a leap year.
flowchart TD
A([Start]) --> B[/Read year/]
B --> C{"year ÷ 4 == 0?"}
C -->|Yes| D{"year ÷ 100 == 0?"}
D -->|Yes| E{"year ÷ 400 == 0?"}
E -->|Yes| F[/Leap year/]
E -->|No| G[/Not a leap year/]
D -->|No| F
C -->|No| G
F --> H([End])
G --> HHow the C Program to Check Leap Year Works
Let's break down the code and see how it checks for leap years.
#include <stdio.h>: Includes the standard input and output library to facilitateprintfandscanffunctions.int main(): Defines the starting point of the program.int year;: Declares a variable to store the year input by the user.scanf("%d", &year);: Reads the user input and stores it in the variableyear.- The
ifcondition checks whetheryear % 4 == 0andyear % 100 != 0or ifyear % 400 == 0, indicating a leap year. - If true, the program prints that the year is a leap year; otherwise, it prints that it is not a leap year.
return 0;: Indicates that the program has executed successfully.
Quick Quiz — test your understanding
Tap an option to check your answerWhat condition must a year satisfy to be a leap year?
Time and Space Complexity of the C Program to Check Leap Year
Analyzing the performance of this program:
Time Complexity: O(1). Since this program only performs a fixed number of operations regardless of the input year, the time complexity is constant. For any given year, the code evaluates a set of logical conditions and executes one printf.
Space Complexity: O(1). The program uses a single integer variable year, requiring a constant amount of space.
Common mistake:Not checking the condition year % 400 == 0 after confirming year % 100 == 0 can lead to wrong results.
Key takeaway:Make sure all leap year conditions are checked correctly for accurate results.
FAQs About Checking Leap Year in C
Here are some common questions students have when working on this program.
if conditions can be mixed and nested in different ways, but remember, clarity matters more than brevity for teaching. Related Programs
If you found this program helpful, check out these related programs:
- C Program to Find the Area and Perimeter of a Rectangle
- C Program to Calculate Area and Circumference of Circle
- C Program to Find the Roots of a Quadratic Equation
- C Program to Calculate Standard Deviation — Full Code & Output
- C Program to Calculate Simple and Compound Interest
Conclusion
Leap years help keep our calendar in sync with the Earth’s revolutions. They're important for date calculations. Try tweaking the program to check multiple years. For more C fun, dive into programs involving loops and arrays. You’ll run into similar conditional checks often.
Test your C skills with our C programming quiz and run your code in the C online compiler.