0
0
CProgramBeginner · 2 min read

C Program to Check Leap Year with Output and Explanation

A C program to check leap year uses the condition if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) to decide if a year is leap and prints the result accordingly.
📋

Examples

Input2000
Output2000 is a leap year.
Input1900
Output1900 is not a leap year.
Input2024
Output2024 is a leap year.
🧠

How to Think About It

To check if a year is leap, first see if it is divisible by 400. If yes, it is leap. Otherwise, check if it is divisible by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
📐

Algorithm

1
Get the year as input from the user.
2
Check if the year is divisible by 400; if yes, it is a leap year.
3
Else, check if the year is divisible by 4 and not divisible by 100; if yes, it is a leap year.
4
Otherwise, it is not a leap year.
5
Print the result.
💻

Code

c
#include <stdio.h>

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);

    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}
Output
Enter a year: 2024 2024 is a leap year.
🔍

Dry Run

Let's trace the input year 2024 through the code

1

Input year

year = 2024

2

Check divisibility by 400

2024 % 400 = 24 (not zero), so false

3

Check divisibility by 4 and not by 100

2024 % 4 = 0 (true) and 2024 % 100 = 24 (not zero), so true

4

Decide leap year

Condition is true, so 2024 is leap year

5

Print result

Output: '2024 is a leap year.'

StepConditionResult
Check year % 400 == 02024 % 400 == 0False
Check year % 4 == 02024 % 4 == 0True
Check year % 100 != 02024 % 100 != 0True
Final leap year condition(False) || (True && True)True
💡

Why This Works

Step 1: Divisible by 400 means leap year

If a year divides evenly by 400, it is always a leap year, so we check year % 400 == 0 first.

Step 2: Divisible by 4 but not by 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year, so we check year % 4 == 0 && year % 100 != 0.

Step 3: Otherwise, not a leap year

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using nested if-else
c
#include <stdio.h>

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);

    if (year % 400 == 0) {
        printf("%d is a leap year.\n", year);
    } else if (year % 100 == 0) {
        printf("%d is not a leap year.\n", year);
    } else if (year % 4 == 0) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}
This approach uses nested if-else for clearer step-by-step checks but is longer.
Using a function to check leap year
c
#include <stdio.h>

int isLeapYear(int year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);

    if (isLeapYear(year)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}
This approach separates logic into a function for reuse and clarity.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

The program uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using a function improves readability but does not affect speed.

ApproachTimeSpaceBest For
Single if conditionO(1)O(1)Simple and concise code
Nested if-elseO(1)O(1)Clear stepwise logic
Function-based checkO(1)O(1)Code reuse and clarity
💡
Always check divisibility by 400 first because it overrides the other conditions.
⚠️
Beginners often forget to exclude years divisible by 100 but not by 400 as leap years.