Write a C Program to check whether a given year is a leap year or not.
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).
- Take a year as input.
- Check whether a given year is divisible by 400.
- Check whether a given year is divisible by 100.
- Check whether a given year is divisible by 4.
- If the condition at step 2 and 4 becomes true, then the year is a leap year.
- 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:
- Leap Year Program in C using Multiple else-if Statements
- Leap Year Program in C using Single if-else Statement
- Leap Year Program in C using Function
- Leap Year Program in C using Macro
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.
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.
/* * 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); }
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.
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
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.
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.
/* * 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); }
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.
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
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.
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; }
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.
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.
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
In this method, we will use a C macro to check whether a year is a leap year.
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; }
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.
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.
- Check C Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Check Computer Science Books