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 Function

  1. Apr 18, 2008 #1
    1. The problem statement, all variables and given/known data

    Write a function that prompts the user to enter a year and that returns a 1 if the entered year is a leap year, and a 0 otherwise.

    A year is a leap year if it is divisible by 4.
    In general, a year is not a leap year if it is divisible by 100, unless of course it is divisible by 400.

    3. The attempt at a solution

    I have done this much and when I test my program, it seems to return a value of 1 no matter what year I enter. I'm pretty awful at programming and I was wondering if anyone could spot where my mistake is.


    #include <iostream>

    using namespace std;

    bool leap(int) {
    int n;
    bool b;

    if (n%4==0) { b=true; } // if n is divisible by 4, it is a leap year
    else if (n%100==0) {
    if (n%400==0) { b=true; }
    else { b=false; }
    } // if n is divisible by 100, check if it is divisible by 400, if it is, it is a leap year
    else { b=false; } // if n is not divisible by 4 (=> it is not divisible by 400 either), then it is not a leap year

    return b;
    }

    int main () {
    int n;
    cout << "Please enter a year: " << '\n';
    cin >> n;

    cout << leap(n);

    }
     
  2. jcsd
  3. Apr 18, 2008 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Wrong definition, so you get the wrong answer.

    A year is a leap year if it is divisible by 4 and it either is not divisible by 100 or it is divisible by 400.
     
  4. Apr 18, 2008 #3

    HallsofIvy

    User Avatar
    Science Advisor


    This will only be accessed if n is not divisible by 4 which, if n is divisible by 100, is impossible.
    This says, if n is divisible by 400, b= true. Under any other condition, and it doesn't matter what happend if the first "if", b will be equal to false. In other words, if n is any number that isnot a multiple of 400, you return "false".
    What you have written will return "true" if n is divisible by 400, otherwise it will return "false".
     
  5. Apr 18, 2008 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Be careful with your "else" statements. You are using at least one "else" statement in an unintended way.

    - Warren
     
  6. Sep 20, 2011 #5
    is this close to the solution?

    using namespace std;
    int main () {
    //declare variable integer for year, y;
    int y, r1, r2, r3;
    //ask user to input a year;
    cout << "Enter a year and press ENTER " << endl;
    //place year in int y;
    cin >> y;
    //divide year by 4 and find remainder;
    r1 = y % 4;
    r2 = y % 400;
    r3= y % 100;
    //main if statement: if rem of y/4 is 0 divide by 400, if there is no rem pri\
    nt y is not leap year and end prg;
    if (r1==0) {r2 = y % 400;
    // if rem of y/400 is 0 then print y is leap year and end prg, otherwise divi\
    de y/100;
    if (r2==0) {cout << y << " is a leap year." << endl;
    //if rem of y/100=0 cout y is not leap year, end prg, otherwise print y is \
    leap year, end prg;
    else if (r3==0) {cout << y << " is NOT a leap year." << endl;
    else cout << y << " is a leap year" <<endl;}}}
    else cout <<y<< " is NOT a leap year." << endl;
    return 0;
    }
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook