Learn to write a C program for a leap year through various methods like loops, if-else statements, switch statements, conditional operators, and more. Firstly, let us know about what is a leap year. Generally, a leap year comes every four years and consists of one extra day.
PW Skills Lab is a online compiler available for free. Practice programming, save your progress and code anywhere. Let us learn to write a C program using various methods.
What is Leap Year?
A leap year is different from other years because of the extra day it holds. Every leap year consists of 366 days, i,e one day more than 365 days. A leap year comes once every four years, with February having 29 days instead of 28 days. This additional day in February makes the year a Leap Year.
Some examples of leap years are 1992, 1996, 1998, 2000, etc. There are certain conditions that are used to check whether a year is a leap year or not.
- The given year must be divisible by 4. For instance, 1998 is divisible by 4, so it can be a leap year.
- The given year must be divisible by 400 and not divisible by 100. For instance, 1998 is also divisible by 400 but not by 100.
You can use these conditions in the code to check whether a given year is a leap year or not. Let us learn to calculate leap years using various methods in the C program.
C Program for A Leap Year Using If-Else Statement
The first solution that comes to mind while writing a leap year program is a conditional statement. Let us use the if-else statement to write a C program for a leap year.
C Program for A Leap Year Using If-Else Method |
#include <stdio.h>
int main() { int year; printf (“Enter a year: “); //Display message on the screen scanf (“%d”, &year); if (year % 400 == 0) { //If-else condition printf(“%d is a leap year.\n”, year); } else if (year % 100 == 0) { printf(“%d is not a 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); } return 0; } |
The given program will check the given year for every condition and return the answer. For instance, let us take the year 2000 as an example. Inside the if-else statement, we will first check whether 2000 is divisible by 400, if Yes we will print a display message showing “ The given year 2000 is a leap year”.
Now, we will check whether 2000 is divisible by 100 if it satisfies then the year is not a leap year, if not we will check with 3 and if it is a yes, then the given year is a leap year.
C Program For A Leap Year Using Switch Statements
Let us use switch statements to write a c program for a leap year.
C Program for A Leap Year Using Switch Method |
#include <stdio.h>
int main() { int year; printf(“Enter a year: “); scanf(“%d”, &year); switch (year % 400 == 0) { case 1: printf (“%d is a leap year.\n”, year); break; case 0: switch (year % 100 == 0) { case 1: printf (“%d is not a leap year.\n”, year); break; case 0: switch (year % 4 == 0) { case 1: printf (“%d is a leap year.\n”, year); break; case 0: printf (“%d is not a leap year.\n”, year); break; } break; } break; } return 0; } |
C Program For A Leap Year Using Ternary Operator
A ternary operator is a type of conditional operator in programming that takes into use three operands and evaluates an expression based on a given condition.
C Program for A Leap Year Using Ternary Operator |
#include <stdio.h>
int main() { int year; printf(“Enter a year: “); scanf(“%d”, &year); (year % 400 == 0) ? printf(“%d is a leap year.\n”, year) : (year % 100 == 0) ? printf(“%d is not a leap year.\n”, year) : (year % 4 == 0) ? printf(“%d is a leap year.\n”, year) : printf(“%d is not a leap year.\n”, year); return 0; } |
C Program For A Leap Year Using Loops
Using for loops and conditional statement to check whether a given year is a leap year.
C Program for A Leap Year Using Ternary Operator |
#include <stdio.h>
int main() { int startYear, endYear; printf(“Enter start year: “); scanf(“%d”, &startYear); printf(“Enter end year: “); scanf(“%d”, &endYear); for (int year = startYear; year <= endYear; year++) { if (year % 400 == 0) { printf(“%d is a leap year.\n”, year); } else if (year % 100 == 0) { printf(“%d is not a 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); } } return 0; } |
C Program for A Leap Year Using Function
Let us create a user defined function isLeapYear() to check whether the given year is a leap year.
C Program for A Leap Year Using Ternary Operator |
#include <stdio.h>
int isLeapYear(int year) { if (year % 400 == 0) { return 1; } else if (year % 100 == 0) { return 0; } else if (year % 4 == 0) { return 1; } else { return 0; } } int main() { int year; printf(“Enter a year: “); scanf(“%d”, &year); if (isLeapYear(year)) { printf(“%d is a leap year.\n”, year); } else { printf(“%d is not a leap year.\n”, year); } return 0; } |
C Program to For a Leap Year Time and Space Complexity
Let us check the time complexity and space complexity for methods we are using for writing a C program for a leap year.
Method | Time Complexity | Space Complexity |
If-Else method | O(1) | O(1) |
Switch Statement | O(1) | O(1) |
Ternary Operator | O(1) | O(1) |
Using Loop | O(n) | O(1) |
Using Function | O(1) | O(1) |
Learn Programming with PW Skills
Master C++ programming and DSA with PW Skills Decoding C++ with DSA Course. Learn fundamentals of C++ programming with real world practice problems and assessments. Learn to write optimized codes with the help of efficient data structures like Array, stacks, Queues, graphs, trees and more. Become a certified programmer at pwskills.com
C Program For A Leap Year FAQs
Q1. How to calculate a leap year?
Ans: If a given year is divisible by 4 or the number is divisible by 400 then it is a leap year. For instance, 2000 is a leap year.
Q2. Why is 1700 not a leap year?
Ans: The year which is divisible by 4 except end of the century years which is not evenly divisible by 400 are leap year. Therefore, 1700 is not a leap year.
Q3. How is 2024 a leap year?
Ans: The year 2024 is a leap year because it is evenly divisible by 4 except end of the centuries years which is not evenly divisible by 400. 2020, 2024 and 2028 are leap year as it comes once every 4 years where February consists of 29 days.