A leap year is a year that is divisible by 4 but not by 100, except if it is also divisible by 400. This ensures that our calendar stays aligned with the Earth’s revolutions around the Sun. In this article, we’ll explore three different ways to write a Leap Year program in C.
Learn different ways to write a “Leap Year program in C”
Method 1) Leap Year program in C using Simple Conditional Statements
This method involves straightforward if-else conditions to check the leap year criteria.
Code Example
#include <stdio.h> int main() { int year; // Input year from user printf("Enter a year: "); scanf("%d", &year); // Check if the year is a leap year if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { printf("%d is a leap year.\n", year); } else { printf("%d is not a leap year.\n", year); } return 0; }
Explanation of the Code
- Input Year: The user is prompted to enter a year, which is stored in the variable
year
. - Condition Check: The
if
statement checks two conditions:- If the year is divisible by 4 and not by 100.
- If the year is divisible by 400.
- Output: Based on the condition, the program prints whether the year is a leap year or not.
Method 2) Leap Year program in C using a Function to Check Leap Year
This method defines a function to determine if a year is a leap year, making the main function cleaner and more modular.
Code Example
#include <stdio.h> // Function to check if a year is a leap year int isLeapYear(int year) { if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return 1; // Leap year } else { return 0; // Not a leap year } } int main() { int year; // Input year from user printf("Enter a year: "); scanf("%d", &year); // Use the function to check if the year is a leap year if (isLeapYear(year)) { printf("%d is a leap year.\n", year); } else { printf("%d is not a leap year.\n", year); } return 0; }
Explanation of the Code
- Function Definition: The
isLeapYear
function encapsulates the logic to determine if a year is a leap year. - Input Year: Similar to the previous method, the user inputs a year.
- Function Call: The
isLeapYear
function is called within theif
statement to check the condition. - Output: The result is printed based on the function’s return value.
Method 3) Leap Year program in C using Ternary Operator
This method demonstrates the use of the ternary operator for a more compact solution.
Code Example
#include <stdio.h> int main() { int year; // Input year from user printf("Enter a year: "); scanf("%d", &year); // Check leap year using ternary operator (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? printf("%d is a leap year.\n", year) : printf("%d is not a leap year.\n", year); return 0; }
Explanation of the Code
- Input Year: The user inputs the year as before.
- Ternary Operator: The ternary operator evaluates the leap year condition and prints the result in a single line.
- Output: The program outputs whether the year is a leap year or not based on the ternary operation.
In this article, we explored three different methods to check if a year is a leap year using C programming: simple conditional statements, a dedicated function, and the ternary operator. Each method provides a unique approach to solving the problem, allowing you to choose the one that best fits your coding style and requirements. Understanding these methods enhances your programming skills and prepares you for more complex logical problems.