C program to check Leap Year

Previous Program Next Program

Write a C program to check leap year using if else. How to check whether a given year is leap year or not in C programming. Program to enter year from user and check whether it is leap year or not using if else in C program. Logic to check leap year in C program.

Example

Input

Input year: 2004

Output

2004 is leap year.

Required knowledge

Basic C programming, If else, Logical operators

Logic to check leap year

Before I get down to the logic section of this program. Let me first define what does leap year mean. Wikipedia defines leap year as a special year containing one extra day i.e. total 366 days in a year.

Below is the step by step descriptive logic to check leap year:

  1. If the year is exactly divisible by 4 and not divisible by 100, then it is leap year.
  2. Else if the year is exactly divisible by 400 then it is leap year.
  3. If both the condition does not satisfy, then it is a normal year.

Let us now implement this logic in our program.

Program to check leap year

/**
 * C program to check Leap Year
 */

#include <stdio.h>

int main()
{
    int year;

    /* Read year from user */
    printf("Enter year : ");
    scanf("%d", &year);


    /* Check for leap year conditions */
    if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))
    {
        printf("LEAP YEAR");
    }
    else
    {
        printf("COMMON YEAR");
    }

    return 0;
} 

Enhance your skills by learning this program using conditional operator -

Output
Enter year : 2004
LEAP YEAR

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment