Leap year program in C, C++ and Java | Program to check if a given year is a leap year or not

Leap year program in C, C++ and Java | Program to check if a given year is a leap year or not

Program to check if a given year is a leap year or not

Introduction

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:

  • What is a Leap Year?
  • How to check if a year is a Leap Year?
  • C program to check Leap Year (With & Without Functions)
  • Optimized Code with Explanation
  • Flowchart and Visual Representation

What is a Leap Year?

A year is a leap year if:

  • It is divisible by 4
  • Except if it is also divisible by 100
  • Unless it is also divisible by 400

Examples

  • 2000 (Divisible by 400, so it is a leap year)
  • 1900 (Divisible by 100 but not 400, not a leap year)
  • 2024 (Divisible by 4 but not by 100, so it is a leap year)
  • 2023 (Not divisible by 4, so not a leap year)

Flowchart for Leap Year Program

A flowchart helps visualize the logic:

Decision Making Process

         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.


Method 1: Leap Year Program Without Functions

C Code

#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;
}

Output

Enter a year: 2024
2024 is a Leap Year.
Enter a year: 1900
1900 is not a Leap Year.

Explanation

  • If the year is divisible by 4 and not by 100, OR it is divisible by 400, it is a leap year.
  • Otherwise, it is not a leap year.

Method 2: Leap Year Program Using Functions

C Code

#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;
}

Output

Enter a year: 2000
2000 is a Leap Year.
yamlCopyEditEnter a year: 2023
2023 is not a Leap Year.

Advantages of Using Functions

  • Improves code reusability
  • Increases readability
  • Easier to debug and modify

Method 3: Leap Year Program Using Command-Line Arguments

This method allows the user to enter the year as a command-line argument instead of inputting it manually.

C Code

#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;
}

How to Run in Terminal?

gcc leapyear.c -o leapyear
./leapyear 2024

Output:

2024 is a Leap Year.

Leap Year Program in C++ (Without Functions)

C++ Code

#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;
}

Output

Enter a year: 2024
2024 is a Leap Year.
yamlCopyEditEnter a year: 2023
2023 is not a Leap Year.

Leap Year Program in C++ (Using Functions)

C++ Code

#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;
}

Advantages of Using Functions

  • Improves code reusability
  • Increases readability
  • Easier to debug and modify

Leap Year Program in Java (Without Functions)

Java Code

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.");
}
}

Output

Enter a year: 2024
2024 is a Leap Year.
Enter a year: 2023
2023 is not a Leap Year.

Leap Year Program in Java (Using Methods)

Java Code

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.");
}
}

Leap Year Program in Java (Using Command-Line Arguments)

Java Code

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.");
}
}

How to Run in Terminal?

javac LeapYearCLI.java
java LeapYearCLI 2024

Output:

csharpCopyEdit2024 is a Leap Year.

Comparison of Methods

MethodProsCons
Without Functions (Basic)Simple and easy to understandCode repetition
With Functions (Best)Reusable and modularSlightly more complex
Using Command-Line ArgumentsIdeal for scriptsRequires terminal execution

The best approach is using functions for modularity and command-line arguments for automation.


Frequently Asked Questions (FAQs)

1. Why does a leap year have 366 days?

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.

2. Can the leap year program be optimized further?

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;
}

3. What is the next leap year after 2024?

After 2024, the next leap years are 2028, 2032, 2036, and so on.


Conclusion

This article covered:

  • Understanding Leap Years
  • Different Methods to Check Leap Year in C
  • Code Implementation (Iterative, Functions, CLI)
  • Comparison of Different Approaches

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.

 Program to check if a given year is a leap year or not