FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Check Leap Year using function

noidatut course




This program takes the value of year(entered by user) and checks whether the input year is Leap year or not. The steps to check leap year are as follows: To determine whether a year is a leap year, here are the steps:

Step 1: If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
Step 2: If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
Step 3: If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
Step 4: The year is a leap year (it has 366 days).
Step 5: The year is not a leap year (it has 365 days).
Lets write these steps in a C++ program.

To understand this program, you should have the knowledge of C++ functions and if-else.

Example: Program to check whether the input year is leap year or not

#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






Leave Comment