C program to check Leap Year

Write a C program to check leap year or not using if else. How to check whether a 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.

Example:
Input Year: 2004
Output: Leap year

Required knowledge

Basic C programming, If else, Logical operators

Logic to check leap year

As wikipedia states Leap year is a special year containing one extra day i.e. total 366 days in a year.
If the year is exactly divisible by 4 and not divisible by 100 then its Leap Year
Else if the year is exactly divisible by 400 then its Leap Year
Else its a common year.

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
     */
    if(((year%4 == 0) && (year%100 !=0)) || (year%400==0))
    {
        printf("LEAP YEAR");
    }
    else
    {
        printf("COMMON YEAR");
    }

    return 0;
} 


Also do check this program using another programming approach to enhance your skills - C program to check leap year using conditional operator.


Output
Enter year : 2004
LEAP YEAR

Happy coding ;)


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
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment