Program to Check if a Year is a Leap Year in C

Checking Leap Year

A leap year is a year that is evenly divisible by 4, except for years that are evenly divisible by 100. However, years divisible by 400 are also considered leap years.

We will explore different methods to check if a year is a leap year using C programming.

Method 1: Using if-else Statement

We use an if-else statement to check if a year satisfies the leap year conditions.

#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", year);
    } else {
        printf("%d is not a leap year", year);
    }
    return 0;
}
            

Output:

Enter a year: 2024
2024 is a leap year

Method 2: Using Ternary Operator

We use the ternary operator to check leap year conditions in a single line.

#include <stdio.h>

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);
    
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 
        printf("%d is a leap year", year) : 
        printf("%d is not a leap year", year);
    
    return 0;
}
            

Output:

Enter a year: 2023
2023 is not a leap year

Method 3: Using Function

We create a function to check if a year is a leap year and return the result.

#include <stdio.h>

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

int main() {
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);
    
    if (isLeapYear(year)) {
        printf("%d is a leap year", year);
    } else {
        printf("%d is not a leap year", year);
    }
    
    return 0;
}
            

Output:

Enter a year: 2000
2000 is a leap year
Easy aceess next quctions
Getting Started

Positive or Negative number: C C++ Java Python

Even or Odd number: C C++ Java Python

Sum of First N Natural numbers: C C++ Java Python

Sum of N natural numbers: C C++ Java Python

Sum of numbers in a given range: C C++ Java Python

Greatest of two numbers: C C++ Java Python

Greatest of the Three numbers: C C++ Java Python

Leap year or not: C C++ Java Python

Prime number: C C++ Java Python

Prime number within a given range: C C++ Java Python

Sum of digits of a number: C C++ Java Python

Reverse of a number: C C++ Java Python

Palindrome number: C C++ Java Python

Armstrong number: C C++ Java Python

Armstrong number in a given range: C C++ Java Python

Harshad number: C C++ Java Python

Abundant number: C C++ Java Python

Friendly pair: C C++ Java Python