Create an Leap Year Checker in C++ with Free Codes

Introduction – Leap Year

A Leap Year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400. For example, 2000, and 2400 are leap year, while 1800 and 1900 are not. Implementing a leap year checker in C++ involves understanding basic conditional statements and arithmetic operations.

Algorithm for Checking Leap Year

The algorithm to check if a year is a leap year can be summarized as follows:

  1. Input: Take a year as input.
  2. Conditions:
    • A year is a leap year if it is divisible by 4.
    • However, if the year is divisible by 100, it is not a leap year, unless:
    • The year is also divisible by 400, in which case it is a leap year.
  3. Output: Print whether the year is a leap year or not based on the above conditions.

Project Codes

Here is the Project Codes

#include <iostream>

bool isLeapYear(int year) {
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    } else {
        return false;
    }
}

int main() {
    int year;
    std::cout << "Enter a year: ";
    std::cin >> year;

    if (isLeapYear(year)) {
        std::cout << year << " is a leap year.\n";
    } else {
        std::cout << year << " is not a leap year.\n";
    }

    return 0;
}

Leap Year - E-Books

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top