A leap year is a year that has 366 days instead of 365, with an extra day in February (29th Feb) to keep the calendar in sync with the Earth’s orbit around the Sun. This extra day compensates for the fact that a year is approximately 365.2425 days long instead of exactly 365.
In this article, we will discuss:
A year is a leap year if:
A flowchart helps visualize the logic:
Start
|
Input Year
|
Is year % 4 == 0 ?
/ \
Yes No ---> Not a Leap Year
|
Is year % 100 == 0 ?
/ \
Yes No ---> Leap Year
|
Is year % 400 == 0 ?
/ \
Yes No
Leap Not a
Year Leap Year
A diagram can be included here for better understanding.
#include <stdio.h>
int main() {
int year;
// Input year from user
printf("Enter a year: ");
scanf("%d", &year);
// Leap year logic
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;
}
Enter a year: 2024
2024 is a Leap Year.
Enter a year: 1900
1900 is not a Leap Year.
#include <stdio.h>
// Function to check leap year
int isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}
int main() {
int year;
// Input year from user
printf("Enter a year: ");
scanf("%d", &year);
// Calling function
if (isLeapYear(year))
printf("%d is a Leap Year.\n", year);
else
printf("%d is not a Leap Year.\n", year);
return 0;
}
Enter a year: 2000
2000 is a Leap Year.
yamlCopyEditEnter a year: 2023
2023 is not a Leap Year.
This method allows the user to enter the year as a command-line argument instead of inputting it manually.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <year>\n", argv[0]);
return 1;
}
int year = atoi(argv[1]);
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;
}
gcc leapyear.c -o leapyear
./leapyear 2024
Output:
2024 is a Leap Year.
#include <iostream>
using namespace std;
int main() {
int year;
// Input year from user
cout << "Enter a year: ";
cin >> year;
// Leap year logic
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
cout << year << " is a Leap Year." << endl;
else
cout << year << " is not a Leap Year." << endl;
return 0;
}
Enter a year: 2024
2024 is a Leap Year.
yamlCopyEditEnter a year: 2023
2023 is not a Leap Year.
#include <iostream>
using namespace std;
// Function to check if a year is a leap year
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
// Input year from user
cout << "Enter a year: ";
cin >> year;
// Calling function
if (isLeapYear(year))
cout << year << " is a Leap Year." << endl;
else
cout << year << " is not a Leap Year." << endl;
return 0;
}
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
scanner.close();
// Leap year logic
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
Enter a year: 2024
2024 is a Leap Year.
Enter a year: 2023
2023 is not a Leap Year.
import java.util.Scanner;
public class LeapYearFunction {
// Method to check if a year is a leap year
static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
scanner.close();
// Calling method
if (isLeapYear(year))
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
public class LeapYearCLI {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java LeapYearCLI <year>");
return;
}
int year = Integer.parseInt(args[0]);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
javac LeapYearCLI.java
java LeapYearCLI 2024
Output:
csharpCopyEdit2024 is a Leap Year.
Method | Pros | Cons |
---|---|---|
Without Functions (Basic) | Simple and easy to understand | Code repetition |
With Functions (Best) | Reusable and modular | Slightly more complex |
Using Command-Line Arguments | Ideal for scripts | Requires terminal execution |
The best approach is using functions for modularity and command-line arguments for automation.
A normal year has 365.2425 days, but the calendar counts only 365 days. To adjust this difference, one extra day (February 29) is added every four years.
Yes, we can use ternary operators for a shorter version:
cCopyEdit#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
printf("%d is %s Leap Year.\n", year, (year%4==0 && year%100!=0) || (year%400==0) ? "a" : "not a");
return 0;
}
After 2024, the next leap years are 2028, 2032, 2036, and so on.
This article covered:
This structured approach ensures a clear understanding of the leap year concept and its implementation in C. Let me know if additional examples or visual representations are needed.