Wednesday, 30 October 2013

C++ Program to check whether an year is 'Leap Year or Not'


In this program we will check whether an input year is a leap year or not!

The basic rules a leap year should follow are:

  • A year is a leap year if it is divisible by 400.
    e.g., 1600, 2400 etc., ARE leap years while 1700 or 1100 ARE NOT!
  • If an year is neither divisible by 400 nor by 100, then it IS a leap year, iff it is divisible by 4.
    e.g., 2004, 2016, 1996 etc., ARE leap years while 2007 or 1999 ARE NOT!
Here is the code:
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
#include<iostream.h>
#include<conio.h>
void main()
{
    int year;
    cout<<"Enter the year you want to check : ";
    cin>>year;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
          cout<<"\nYes! It cerainly looks like a leap year!";
    else
          cout<<"\nMhmmm... Nah... I don't think it is a leap year.";
getch();
}
For newer IDEs (like Dev C++) :
/*Support us by subscribing, commenting, and viewing the 
blog frequently! - GeekyCircle Admin :)*/
#include<iostream>
using namespace std;
int main()
{
    int year;
    cout<<"Enter the year you want to check : ";
    cin>>year;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
        cout<<"\nYes! It cerainly looks like a leap year!";
    else
        cout<<"\nMhmmm... Nah... I don't think it is a leap year.";
 return 0;
 }
Working

This program is really simple. First we check whether the given year is divisible by 4 and not by 100. If it is, then we will output "it is a leap year". OR we check if it is divisible by 400, and if it is, we will output "it is a leap year. If both the above conditions are false, we output the "year is not a leap year".

No comments:

Post a Comment

You might also like