5
#include <iostream>
using namespace std;

int main() {
    int what_year;

    cout << "Enter calendar year ";
    cin >> what_year;

    if (what_year - (n * 4) = 0 ) {

        cout << "leap year";
    }

    else
    {
        cout << "wont work";
    }

    system("Pause");
    return 0;
}

Trying to make a program for class, to find a leap year.. not sure how to ask C++ if an integer is divisible by a number?

jww
  • 97,681
  • 90
  • 411
  • 885
user1698658
  • 59
  • 1
  • 1
  • 2
  • As others mention below, what you're looking for is "modular arithmetic". It is an operator that gives you the remainder of a divide. If the remainder is 0, the number is wholly divisible. – Ross Sep 25 '12 at 22:48
  • 1
    Possible duplicate of [How to find leap year programatically in C](http://stackoverflow.com/questions/3220163/how-to-find-leap-year-programatically-in-c) – senegrom May 25 '16 at 12:15
  • A handy collection of low-level date algorithms: http://howardhinnant.github.io/date_algorithms.html#is_leap – Howard Hinnant Jul 27 '16 at 14:18

4 Answers4

9

The leap year rule is

 if year modulo 400 is 0 then
   is_leap_year
else if year modulo 100 is 0 then
   not_leap_year
else if year modulo 4 is 0 then
   is_leap_year
else
   not_leap_year

http://en.wikipedia.org/wiki/Leap_year#Algorithm

You can use the modulo operator to see if one number is evenly divisible by another, that is if there is no remainder from the division.

2000 % 400 = 0 // Evenly divisible by 400

2001 % 400 = 1 // Not evenly divisible by 400

Interestingly, several prominent software implementations did not apply the "400" part, which caused February 29, 2000 not to exist for those systems.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • @KevinP.Rice: Cool optimization. Too bad Wikipedia won't accept that as an addition to the existing article. – Eric J. Jan 31 '13 at 01:28
4

Use the modulo function.

if ((year % 4) == 0)
{
//leap year
}

Note that this does not account for the 100 and 400 year jump.

Proper code would be something like

if(((year%4) == 0) && (((year%100)!=0) || ((year%400) == 0))
{
//leap year
}
SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
  • My code does account for the 400 year leap year. Assume the year is 2400, then it results in (true && (false || true)) = (true && true) = true. – SinisterMJ Oct 12 '15 at 11:42
  • Huh. Yes, your code clearly does the 400 leap year. I don't know why I didn't see that before. :) – Charles Plager Oct 13 '15 at 18:31
4

Use this instead

bool bLeapYear = false;
if ((what_year % 4) ==0)  {
     if ((what_year % 100) == 0) {
          bLeapYear = ((what_year % 400) == 0);
     } else {
          bLeapYear = true;
     }
     // leap year
}

This takes the remainder of the year after dividing by 4 and tests to see if it is zero. You also had a problem using = instead of == - the latter tests for equality, the former assigns a value.

EDIT: Edited according to the comment of Steve below.

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

According to some rules that decide whether a year is leap or not, a year should be divisible by 4 and for those years that are divisible by 100, it should also be divisible by 400.

int year;
cout << "Enter a year: ";
cin >> year;

if (year%4 == 0) {
    if (year%100 == 0) {
        if (year%400 == 0) {
            cout << year << " is a leap year.";
        }
        else {
            cout << year << " is not a leap year.";
        }
    }
    else {
        cout << year << " is a leap year.";
    }
}
else {
    cout << year << " is not a leap year.";
}

return 0;}
Chirag175
  • 11
  • 1