C program to check leap year using Conditional operator

Write a C program to enter any year and check whether year is leap year or not using conditional/ternary operator (?:).

Example

Input

Input year: 2016

Output

2016 is Leap Year

Required knowledge

Basic C programming, Conditional operator

Read more - Program to check leap year using if else

Leap year condition

If the year is EXACTLY DIVISIBLE by 4 and NOT DIVISIBLE by 100 then its LEAP YEAR
Else if the year is EXACTLY DIVISIBLE 400 then its LEAP YEAR
Else its a COMMON YEAR

Program to check leap year

/**
 * C program to check leap year using conditional operator
 */

#include <stdio.h>

int main()
{
    int year;
 
    /*
     * Input year from user
     */
    printf("Enter any year: ");
    scanf("%d", &year);

    (year%4==0 && year%100!=0) ? printf("LEAP YEAR") :
        (year%400 ==0 ) ? printf("LEAP YEAR") : printf("COMMON YEAR");

    return 0;
}

Note: We can also write the same program using conditional operator as

/**
 * C program to check leap year using conditional operator
 */

#include <stdio.h>

int main()
{
    int year;
 
    /*
     * Input year from user
     */
    printf("Enter any year: ");
    scanf("%d", &year);

    printf("%s", ((year%4==0 && year%100!=0) ? 
                    "LEAP YEAR" : (year%400 ==0 ) ? 
                        "LEAP YEAR" : "COMMON YEAR"));

    return 0;
}

Output

Enter any year: 2016
LEAP YEAR

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>