Friday, September 5, 2025
HomeData Modelling & AIC program to check if a given year is leap year using...

C program to check if a given year is leap year using Conditional operator

Given an integer that represents the year, the task is to check if this is a leap year, with the help of Ternary Operator. A year is a leap year if the following conditions are satisfied:

  1. The year is multiple of 400.
  2. The year is a multiple of 4 and not a multiple of 100.

Following is pseudo-code

if year is divisible by 400 then is_leap_year
else if year is divisible by 100 then not_leap_year
else if year is divisible by 4 then is_leap_year
else not_leap_year

Below is the implementation of the above approach: 

C




// C program to check if a given
// year is a leap year or not
  
#include <stdbool.h>
#include <stdio.h>
  
bool checkYear(int n)
{
  
    return (n % 400 == 0)
               ? true
               : (n % 4 == 0) ? (n % 100 != 0)
                              : false ? true : false;
}
  
// Driver code
int main()
{
    int year = 2000;
  
    checkYear(year) ? printf("Leap Year")
                    : printf("Not a Leap Year");
  
    return 0;
}


Output

Leap Year

Time Complexity: O(1), As only constant time operations are performed.
Auxiliary Space: O(1), As constant extra space is used.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11861 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS