w3resource logo


C Programming Exercises

C Exercises: Check whether a given year is a leap year or not

Secondary Nav

C Conditional Statement: Exercise-4 with Solution

Write a C program to find whether a given year is a leap year or not.
Test Data :
2016 
Expected Output
:
2016 is a leap year. 

Sample Solution :-

C Code:

#include <stdio.h>

void main()
{
    int chk_year;
 
    printf("Input a year :");
    scanf("%d", &chk_year);
    if ((chk_year % 400) == 0)
        printf("%d is a leap year.\n", chk_year);
    else if ((chk_year % 100) == 0)
        printf("%d is a not leap year.\n", chk_year);
    else if ((chk_year % 4) == 0)
        printf("%d is a leap year.\n", chk_year);
    else
        printf("%d is not a leap year \n", chk_year);
}

Flowchart :

Flowchart: Check a given year is leap year or not

Code Editor :

#include <stdio.h>

void main()
{
    int chk_year;
 
    printf("Input a year :");
    scanf("%d", &chk_year);
    if ((chk_year % 400) == 0)
        printf("%d is a leap year.\n", chk_year);
    else if ((chk_year % 100) == 0)
        printf("%d is a not leap year.\n", chk_year);
    else if ((chk_year % 4) == 0)
        printf("%d is a leap year.\n", chk_year);
    else
        printf("%d is not a leap year \n", chk_year);
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program