Leap Year Program in C

Problem Description

Write a C Program to check whether a given year is a leap year or not.

What is a Leap Year?

A leap year is a year with 366 days, instead of the usual 365 days. This is because the Earth takes slightly longer than 365 days to orbit the Sun.

To determine whether a given year is a leap year, you can use the following conditions:

  • The year is exactly divisible by 400 (such as 2000,2400) or,
  • The year is exactly divisible by 4 (such as 2008, 2012, 2016) and not a multiple of 100 (such as 1900, 2100, 2200).
How to Check for a Leap Year in C?
  1. Take a year as input.
  2. Check whether a given year is divisible by 400.
  3. Check whether a given year is divisible by 100.
  4. Check whether a given year is divisible by 4.
  5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
  6. If the condition at step 3 becomes true, then the year is not a leap year.

There are multiple ways to implement a leap year program in C. We’ll explore four methods:

Method 1: (Multiple else-if Statements)

In this approach we will check for Leap Year considering all the cases mentioned in the problem solution one by one using multiple else-if statement.

Program/Source Code

Here is source code of the C program to check whether a given year is leap year. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
advertisement
/*
 * C program to find whether a given year is a leap year or not
 */
 
#include<stdio.h>
void main()
{
    int year;
 
    printf("Enter a year \n");
    scanf("%d", &year);
    if ((year % 400) == 0)
        printf("%d is a leap year \n", year);
    else if ((year % 100) == 0)
        printf("%d is a not leap year \n", year);
    else if ((year % 4) == 0)
        printf("%d is a leap year \n", year);
    else
        printf("%d is not a leap year \n", year);
}
Program Explanation

1. Take a year as input and store it in the variable year.
2. Using if,else statements to,
  a) Check whether a given year is divisible by 400.
  b) Check whether a given year is divisible by 100.
  c) Check whether a given year is divisible by 4.
3. If the condition at step 2.a becomes true, then print the ouput as “It is a leap year”.
4. If the condition at step 2.b becomes true, then print the ouput as “It is not a leap year”.
5. If the condition at step 2.c becomes true, then print the ouput as “It is a leap year”.
6. If neither of the condition becomes true, then the year is not a leap year and print the same.

Example:
consider the year, 2100. Check condition 2.a, when we divide it by 400, we see that the remainder obtained is not 0. Therefore, we print “It is not a leap year”.

Time Complexity: O(1)
The above program for checking Leap Year has a time complexity of O(1) as we use only if else condition and no loops.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Space Complexity: O(1)
In the leap year program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Runtime Test Cases

Testcase 1: Here is the runtime output of a C Program that Checks Whether a given Year “2012” is a Leap Year or not.

 
Enter a year
2012
2012 is a leap year

Testcase 2: In this case, we enter the year “2009” as input to check whether a given year is leap year or not.

Enter a year
2009
2009 is not a leap year

Testcase 3: In this case, we enter the year “2100” as input to check if a given year is a leap year.

advertisement
Enter a year
2100
2100 is not a leap year

Method 2: (One if-else Statement)

In this approach we will check for Leap Year considering all the cases mentioned in the problem solution in a single condition using one if-else statement.

Program/Source Code

Here is the source code of a C program to check if a given year is a leap year or not by using single if else statement. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
/*
 * C program to find leap year using if-else Statement
 */
 
#include<stdio.h>
void main()
{
    int year;
 
    printf("Enter a year \n");
    scanf("%d", &year);
    if (((year % 400) == 0)||(((year%4)==0)&&(year%100)!=0))
        printf("%d is a leap year \n", year);
    else
        printf("%d is not a leap year \n", year);
}
Program Explanation

1. Take a year as input and store it in the variable year.
2. Using a single if, else statement check, if the year is completely divisible by 400 or the year is completely divisible by 4 as well as not divisible by 100.
3. If the above condition is true then print it is a leap year.
4. If the condition becomes false, then the year is not a leap year and print the same.

Example:
Consider a year, 2200. Divide it by 400, we see that the remainder obtained is not 0. Therefore, we print “It is not a leap year”.

Time Complexity: O(1)
The above program for checking Leap Year has a time complexity of O(1) as we use only if else condition and no loops.

Space Complexity: O(1)
In the above program, space complexity is O(1) as no extra variable has been taken to store the values in the memory. All the variables initialized takes a constant O(1) space.

Program Output

Testcase 1: Here is the runtime output of a C program to find the leap year when the year entered by the user is “2012”.

 
Enter a year
2012
2012 is a leap year

Testcase 2: In this case, we enter the year “2009” as input to check whether a given year is leap year or not.

Enter a year
2009
2009 is not a leap year

Testcase 3: In this case, to determine whether a given year is a leap year, we enter the year “2200” as input.

Enter a year
2200
2200 is not a leap year

Method 3: Leap Year Program using Function

A leap year program using a function is a more modular and reusable approach to writing leap year programs. It can also help to improve the readability and maintainability of your code.

Program/Source Code

Here is source code of the C program to check whether a given year is leap year using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * Leap Year Program in C using Function
 */
 
#include <stdio.h>
 
int is_leap_year(int year)
{
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
 
int main()
{
    int year;
 
    printf("Enter a year: ");
    scanf("%d", &year);
 
    if (is_leap_year(year))
    {
        printf("%d is a leap year.\n", year);
    }
    else
    {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}
Program Explanation

1. The is_leap_year() function takes a year as input and returns 1 if the year is a leap year, or 0 if it is not.
2. The function works by checking whether the year is divisible by 400 or by 4 and not a multiple of 100.
3. The main() function prompts the user to enter a year and then calls the is_leap_year() function to check whether the year is a leap year.
4. If the year is a leap year, the program prints a message saying that the year is a leap year.
5. Otherwise, the program prints a message saying that the year is not a leap year.

Time and space complexity:

The time complexity of this program is O(1), because it takes a constant amount of time to execute, regardless of the size of the input. The space complexity of this program is also O(1), because it does not allocate any additional memory.

Program Output

For example, if the user enters the year 2024, the program will print the following output:

Enter a year: 2024
2024 is a leap year

Method 4: Leap Year Program using Macro

In this method, we will use a C macro to check whether a year is a leap year.

Program/Source Code

Here is source code of the C program to check whether a given year is leap year using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * Leap Year Progam in C using macro
 */
 
#include <stdio.h>
 
#define IS_LEAP_YEAR(year) ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
 
int main()
{
    int year;
    printf("Enter a year: ");
    scanf("%d", &year);
 
    if (IS_LEAP_YEAR(year))
    {
        printf("%d is a leap year.\n", year);
    }
    else
    {
        printf("%d is not a leap year.\n", year);
    }
    return 0;
}
Program Explanation

1. The program first prompts the user to enter a year.
2. Then, it uses the macro to check whether the year is a leap year.
3. If the year is a leap year, the program prints a message to the console saying that the year is a leap year.
4. Otherwise, the program prints a message to the console saying that the year is not a leap year.

Program Output

For example, if the user enters the year 2012, the program will print the following output:

Enter a year: 2012
2012 is a leap year

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

Learn C Tutorial and test your knowledge with theseC MCQs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.