1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: C++: Leap Year

  1. Jan 14, 2007 #1
    1. The problem statement, all variables and given/known data
    I just started my C++ courses and here is my question.

    2. Relevant equations

    3. The attempt at a solution
    //This program states whether a given year is a leap year or a non-leap year.

    #include <iostream.h>
    #include <conio.h>

    void main ()
    int year, value;
    char answer,y, n;

    while (answer == y)
    cout << "Please input a year: ";
    value = year/4;
    if (value==int)
    cout << ""<< year << " is a leap year. There are 29 days in Feb " << year <<".";
    cout << ""<< year << " is a non-leap year. There are 28 days in Feb " << year <<".";
    cout << "Do you want to continue? Press 'y' if yes or 'n' if no.";

    It seems that "if (value==int)" is illegal. Is there any other way I can put th expression statement so that the computer can print out whether a given year is a leap year or not?

  2. jcsd
  3. Jan 14, 2007 #2
    What you need to do is to check if the variable year is a multiple of 4 and the modulus operator % is perfect for the task, e.g.,
    1%4 = 1, 2%4 = 2, 3%4 = 3, 4%4 = 0, 5%4 = 1, ...

    What should the conditional expression be for the if-statement now then?

    Btw, the rules for checking a leap year is a bit more involved than the above. Look http://en.wikipedia.org/wiki/Leap_year" [Broken] for an insight.
    Last edited by a moderator: May 2, 2017
  4. Jan 16, 2007 #3
    Okay, thanks for the help. My instructor just introduced the modulus operator today.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook