C++ Program to check whether given year is leap year or not [DEVCPP/GCC]


In the Gregorian calendar(internationally accepted calendar),the following criteria must be taken into consideration to identify leap years:
1. Year must be completely divisible by 4;
2. If the year is divisible by 100, then it must be divisible by 400
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.

//Program to check whether given year is leap year or not

#include <iostream>

using namespace std;

int main()
{

         int year,flag=1;
         cout << "ENTER YEAR: ";
         cin >> year;

         if (year % 4 == 0)
        {
                  flag=0;

                 if((year % 100 == 0) && (year % 400 != 0))
                {
                      flag=1;  //flag=1 represents the second condition as false
                }
        }

      /*If flag remains 0 after execution of above block, that implies both the conditions are satisfied */

        if(flag==0)                   // If both conditions are true
       {
                  cout <<year << " IS A LEAP YEAR" << endl;
       }
       else                               // if either of condition is false
       {
                 cout <<year << " IS NOT A LEAP YEAR" << endl;
        }

}



Share this

Related Posts

FIND US ON FACEBOOK!