(adsbygoogle = window.adsbygoogle || []).push({}); 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);

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: C++ Leap Year Function

**Physics Forums | Science Articles, Homework Help, Discussion**