w3resource

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


4. Leap Year Determination

Write a C program to find whether a given year is a leap year or not.

Visual Presentation:

Check whether a given year is a leap year or not


Sample Solution:

C Code:

#include <stdio.h>   // Include the standard input/output header file.

void main()
{
    int chk_year;   // Declare an integer variable 'chk_year'.

    printf("Input a year :");   // Prompt the user to input a year.
    scanf("%d", &chk_year);   // Read and store the user's input in 'chk_year'.
    if ((chk_year % 400) == 0)   // Check if 'chk_year' is divisible by 400 with no remainder.
        printf("%d is a leap year.\n", chk_year);   // Print a message indicating that 'chk_year' is a leap year.
    else if ((chk_year % 100) == 0)   // Check if 'chk_year' is divisible by 100 with no remainder.
        printf("%d is not a leap year.\n", chk_year);   // Print a message indicating that 'chk_year' is not a leap year.
    else if ((chk_year % 4) == 0)   // Check if 'chk_year' is divisible by 4 with no remainder.
        printf("%d is a leap year.\n", chk_year);   // Print a message indicating that 'chk_year' is a leap year.
    else
        printf("%d is not a leap year \n", chk_year);   // Print a message indicating that 'chk_year' is not a leap year.
}

Output:

Input a year :2016                                                                                            
2016 is a leap year.

Flowchart:

Flowchart: Check a given year is leap year or not


For more Practice: Solve these Related Problems:

  • Write a C program to check a range of years and list all the leap years within that range.
  • Write a C program to determine if a given year is a leap year using a single complex expression.
  • Write a C program to validate a leap year by handling century years correctly (divisible by 400).
  • Write a C program to determine leap years and output a special message if the year is a century but not divisible by 400.

C Programming Code Editor:



Previous: Write a C program to check whether a given number is positive or negative.
Next: Write a C program to read the age of a candiadte and detrermine whether it is eligible for casting his/her own vote.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.