HomeC++C++ Program to Check for Leap Year

C++ Program to Check for Leap Year

In this post, you’ll learn how to Check for Leap Year using C++ programming language.

In this lesson, you will learn how to check if a year is a leap year or not, with the logical operators, equal-to operator, mathematical operations and decision making statements using the C++ language. Let’s look at the below source code.

How to Check for Leap Year in C++?

RUN CODE SNIPPET

Source Code

#include<iostream>
using namespace std;
int main() 
{
   int year;
   cin>> year;
   cout<< "Enter a year: "<< year << endl;
   if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) 
   cout<<year<<" is a leap year";
   else
   cout<<year<<" is not a leap year";
 return 0;
}

Input

2020

Output

Enter a year: 2020
2020 is a leap year

#include <iostream>

  • This line which is called the header file. #include statement tells the compiler to use available files and <iostream>is the name of the specific that we have used in this code. The <iostream> file stands for Input and Output statement.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

A leap year has an additional day in the month of February wand the year that is divisible by 4 is a leap year. A leap year occurs every four years unless it is divisible by 100.   Those years are not leap years unless they are also divisible by 400.   That is why in the chart above 1900, 2100, 2200, 2300 are not Leap Years but 2000 and 2400 are Leap Years. That is the years 2000 and 2400 are leap years whereas 2100, 2200,2300 are not.

We can conclude finally that a leap year occurs once in four years and not all the ending years of a century are leap years. Hence using the above condition we can build our code.

  1. First we declare the variable year as a integer, and we obtain the year from the user and display and store the value in the variable using the cout<< and cin>> functions.
  2. Using the decision making if else statements we declare the conditions to be satisfied and the output statements to be displayed.
  3. (year % 4 == 0) && (year % 100 != 0)  in this condition we first divide the year by 4 and then see if the value is equal to zero. In the same way we divide the year by 100 and see if the reminder is not equal to zero.
  4.  The % symbol is used to find the reminder of two numbers and the && is a logical AND operator that is both the conditions should be true. == is equal to sign and != is not equal to sign.
  5. (year % 400 == 0) in this condition we first divide the year by 4oo and then see if the reminder is equal to 0. || is a logical OR operator, that is any one of the conditions can be true when using this operator.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured with nested if else statements, and the same conditions. Let’s look at the source code below.

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main() 
{
   int year;
   cin>> year;
   cout<< "Enter a year: "<< year << endl;
   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;
}

Input

2020

Output

Enter a year: 2020
2020 is a leap year
  • In the above program, if the year is divisible by 4, then it is checked if it is divisible by 100.
  • If it is divisible by 100, then it is checked if it is divisible by 400. If yes, then the year is a leap year. Otherwise, it is not.
  • If the year is not divisible by 100, it is a leap year. If the year is not divisible by 4, it is not a leap year.
  • In this if the condition is not satisfied the function is stopped and the else statement is executed and the output is displayed.
  • The rest of the statements and functions are similar, to Check for Leap Year in C++.

Share:

Leave A Reply

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

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022