Tuesday, April 4, 2023

Write a C++ Program to Check Leap Year.

C++ Program to Check Leap Year Write a C++ Program to Check Leap Year.

What is a Leap Year?

A leap year is a year that is exactly divisible by 4, except for years that are divisible by 100. However, years that are divisible by 400 are also leap years.


#include <iostream>
using namespace std;

int main() {
    int year;

    cout << "Enter a year: ";
    cin >> year;

    // Leap year logic
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0)
                cout << year << " is a leap year";
            else
                cout << year << " is not a leap year";
        } else
            cout << year << " is a leap year";
    } else
        cout << year << " is not a leap year";

    return 0;
}


//===========
OUTPUT:
Enter a year: 2020
2020 is a leap year

Enter a year: 1900
1900 is not a leap year

Enter a year: 2000
2000 is a leap year


In this program, we first take input of the year from the user. Then we use the leap year logic to check if the year is a leap year or not.

If the year is divisible by 4 and not divisible by 100, it is a leap year. If the year is divisible by 100, it is a leap year only if it is also divisible by 400. If the year is not divisible by 4, it is not a leap year.

Program Explanation

  1. We start by including the <iostream> library which allows us to take input and output operations in C++.
  2. We declare a variable year which will hold the value of the year to be checked.
  3. We use the cout statement to ask the user to enter the year.
  4. We use the cin statement to take input of the year from the user and store it in the year variable.
  5. We use the leap year logic to check whether the year is a leap year or not. We start by checking if the year is divisible by 4 using the modulus operator %. If the remainder of the division is 0, it means the year is divisible by 4.
  6. If the year is divisible by 4, we need to check if it is also divisible by 100. If the year is divisible by 100, it is not a leap year, unless it is also divisible by 400. If the year is not divisible by 100, it is a leap year.
  7. We use if and else statements to print out the result of the check. If the year is a leap year, we print out a message saying so. If it is not a leap year, we print out a message saying so.
  8. Finally, we use the return 0 statement to exit the program.

Program Notes

  1. The using namespace std statement is used to avoid having to write std:: before every standard library function or object.
  2. We use the cout statement to display a message on the screen. The << operator is used to insert the message into the output stream.
  3. We use the cin statement to take input from the user. The >> operator is used to extract the input from the input stream.
  4. The modulus operator % is used to find the remainder of a division.
  5. The if statement is used to check if a condition is true. If the condition is true, the statements within the if block are executed. If the condition is false, the statements within the else block are executed.
  6. The return 0 statement is used to indicate that the program has successfully completed execution.

No comments:

Post a Comment

Write a c++ program to show the difference between an array and a list.

Array vs List Example Write a c++ program to show the difference between an array and a list. Arrays are useful when you know the number ...