C Program to Check Given Year is Leap Year or Not

A leap year is a year containing one additional day in order to keep the calendar year in sync with the astronomical year. Each leap year lasts 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days.

Example of leap years: 1980, 1984, 1988, 1992, 1996, 2000

Algorithm to check whether a year is leap year or not
  • If a year is divisible by 4 but not by 100, then it is a leap year.
  • If a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.

Importance of Practicing the Program for Checking Leap Years for Beginners

  • Understanding of Leap Years : Implementing this program helps beginners understand the concept of leap years and the rules for determining whether a year is a leap year or not.

  • Understanding of Conditional Logic : Writing this program helps beginners understand the concept of conditional logic and how to use it in programming. It improves their ability to write code that can make decisions based on different conditions.

  • Enhanced Problem-solving Skills : Implementing this program enhances problem-solving skills by challenging beginners to think about how to determine whether a year is a leap year or not using C programming concepts. It improves their ability to analyze and solve problems.

  • Introduction to Conditional Statements : This program introduces beginners to conditional statements, such as if-else statements, in C programming. It demonstrates how to use these statements to make decisions based on certain conditions.

  • Preparation for Advanced Topics : The skills and concepts learned from this program prepare beginners for more advanced topics in C programming, such as working with dates, times, and complex algorithms. It lays the foundation for understanding more complex programming concepts.

  • Real-world Applications : Understanding leap years is important for various real-world applications, such as calendar systems and date calculations. This program provides a practical example of how programming can be used to solve real-world problems.

C program to check whether a year is leap year or not

This program takes a year as input from user and checks whether it is leap year or not as per above mentioned algorithm and print it accordingly.

#include <stdio.h>

int main() {
    int year;
    printf("Enter a year for leap year check\n");
    scanf("%d", &year);

    if(year%4 != 0){
        printf("%d is not a leap year\n", year);
    } else {   
        if(year%100 == 0){
            if ( year%400 == 0){
                printf("%d is a leap year\n", year);
            } else {
                printf("%d is not a leap year\n", year);
            }
        } else {
            printf("%d is a leap year\n", year );
        }
    }
    return 0;
}
Output
Enter a year for leap year check
1983
1983 is not a leap year
Enter a year for leap year check
2016
2016 is a leap year

C program to check whether a year is leap year or not in one line

#include <stdio.h>
 
int main(){
    int year;
    printf("Enter a year for leap year check\n");
    scanf("%d", &year);

    if(((year%4==0)&&(year%100!=0))||(year%400==0)){
        /* Entered year is a leap year */
        printf("%d is leap year\n", year);
    } else {
        /* Entered year is not a leap year */
        printf("%d is not leap year\n", year);
    }
    return 0;
}
Output
Enter a year for leap year check
2017
2017 is not leap year

Tips for Writing the Program to Check if a Given Year is a Leap Year or Not in C

  • Use Conditional Statements : Use if-else statements to check if the given year is a leap year or not based on the leap year rules.

  • Year Range : Be mindful of the range of years for which the program should work correctly.

  • Consider All Cases : Consider all the cases for determining leap years, such as years divisible by 4, 100, and 400.

  • Algorithm Efficiency : Consider using an efficient algorithm, such as the Gregorian calendar rules for leap years, to determine leap years.

  • Handle User Input : Validate user input to ensure that the entered value is a valid year.

  • Corner Cases : Consider testing the program with corner cases, such as negative years or extremely large years, to ensure its correctness.

Conclusion

In conclusion, the C Program to Check if a Given Year is a Leap Year or Not is a fundamental exercise that introduces beginners to conditional statements and basic arithmetic operations in C programming. Practicing this program is important for beginners to develop a solid understanding of these concepts, which are essential for more advanced programming tasks.

Related Topics
C program to check a number is palindrome or not
C program to find hcf and lcm of two numbers
C program to check string is palindrome
C program to remove vowels from a string
C program to check if two strings are anagram
C program to check armstrong number
C Program to find nPr and nCr
List of all C programs