Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C++
Article

Leap Year

Rate me:
Please Sign up or sign in to vote.
1.00/5 (14 votes)
17 Feb 2004 71K   12   6
This code will calculate whether the year you enter is a leap year or not.

Introduction

This code will calculate whether the year you enter is a leap year or not.

Using the code

This code is VERY straightforward, no tricks here.

//#include "iostream.h"

int year;

int main()

{
    cout << "Please enter the current year: ";
    cin >> year;

    switch (year % 4)
    {
    case 0:
        if (year % 100 == 0)
        {
            cout << "\"Century\" years aren't leap years.";
            if (year % 400 == 0)
            {
                cout << "..unless divisible by 400.\n";
                cout << year << "'s a leap year!" << endl;
            }
            else
                cout << "  " << year << " isn't a leap year." << endl;
        }
        else
            cout << year << " is a leap year!" << endl;
        break;

    case 3:
        cout << "Next year is a leap year.  "; // Fall through...
    default:
        cout << year << " isn't a leap year." << endl;
        break;
    }
    return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUnless your name is Brandon Quick... Pin
Ant19-Feb-04 16:43
Ant19-Feb-04 16:43 
it looks to me like you're nothing more than a plagiarizer of bad code:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7542&lngWId=3


GeneralRe: Unless your name is Brandon Quick... Pin
slimtim19-Feb-04 18:48
slimtim19-Feb-04 18:48 
GeneralRe: Unless your name is Brandon Quick... Pin
HackerBoy23-Feb-04 3:13
HackerBoy23-Feb-04 3:13 
QuestionHow About Pin
Larry Antram18-Feb-04 23:24
Larry Antram18-Feb-04 23:24 
GeneralReally impressive Pin
Patje18-Feb-04 21:32
Patje18-Feb-04 21:32 
GeneralA simpler way Pin
Mark Focas18-Feb-04 17:08
Mark Focas18-Feb-04 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.