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).
Write a C Program to check whether a given year is a leap year or not.
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:
- C Program to find Leap Year using Multiple else-if Statements
- C Program to find Leap Year using Single if-else Statement
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
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.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Buy Computer Science Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship