# Leap Year Program in C

«
»

Leap Year Program in C: A year is a Leap Year if it satisfies 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).
Problem Description

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

Problem Solution

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.

We can solve the leap year problem in two ways. They are:

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
1. `/*`
2. ` * C program to find whether a given year is a leap year or not`
3. ` */`
4. ` `
5. `#include<stdio.h>`
6. `void main()`
7. `{`
8. `    int year;`
9. ` `
10. `    printf("Enter a year \n");`
11. `    scanf("%d", &year);`
12. `    if ((year % 400) == 0)`
13. `        printf("%d is a leap year \n", year);`
14. `    else if ((year % 100) == 0)`
15. `        printf("%d is a not leap year \n", year);`
16. `    else if ((year % 4) == 0)`
17. `        printf("%d is a leap year \n", year);`
18. `    else`
19. `        printf("%d is not a leap year \n", year);`
20. `}`
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.

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.

```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.

1. `/*`
2. ` * C program to find leap year using if-else Statement`
3. ` */`
4. ` `
5. `#include<stdio.h>`
6. `void main()`
7. `{`
8. `    int year;`
9. ` `
10. `    printf("Enter a year \n");`
11. `    scanf("%d", &year);`
12. `    if (((year % 400) == 0)||(((year%4)==0)&&(year%100)!=0))`
13. `        printf("%d is a leap year \n", year);`
14. `    else`
15. `        printf("%d is not a leap year \n", year);`
16. `}`
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```

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. 