C++

Using A Function, A C++ Program Can Check The Leap Year

This application asks the user to enter a year value, and then determines whether or not it is a leap year. The following are the steps to check the leap year:
Here are the steps to figure out if a year is a leap year: Origin of these actions

  • Step 1: Proceed to step 2 if the year can be divided evenly by 4. If not, proceed to step 5.
  • Step 2: Continue to step 3 if the year can be divided evenly by 100. If not, proceed to step 4.
  • Step 3: Continue to step 4 if the year can be divided evenly by 400. If not, proceed to step 5.
  • Step 4: It’s a leap year this year (it has 366 days).
  • Step 5:: The year is not a leap year (it has 365 days).

Let’s create this procedure as a C++ program.

Program to determine whether the input year is a leap year, for instance
You need to be familiar with if-else statements and C++ functions in order to comprehend this program.

#include <iostream>
using namespace std;

bool leapYear(int y);

int main(){
   int y;
   cout<<"Enter year: ";
   cin>>y;
   //Calling function
   bool flag = leapYear(y);
   if(flag == true)  
      cout<<y<<" is a leap Year"; 
   else 
      cout<<y<<" is not a leap Year";
   return 0;
}
bool leapYear(int y){
   bool isLeapYear = false;
   if (y % 4 == 0) {
      if (y % 100 == 0) {
         if (y % 400 == 0) {
            isLeapYear = true;
         }
      } 
      else isLeapYear = true;
   }
   return isLeapYear;
}

Output:

Enter year: 2016
2016 is a leap Year

 

Related Articles

Leave a Reply

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

Back to top button