> what_ye...">

Leap Year Calculation - Homework

#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 the class, find a leap year. Not sure how to ask C ++ if an integer is divisible by a number?

+6
source share
4 answers

Leap Year Rule

  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 find out if one number is equal to divisible by another, that is, if there is no remainder from division.

2000% 400 = 0 // Evenly divided by 400

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

Interestingly, several well-known software implementations did not use the "400" part, which they did not create on February 29, 2000 for these systems.

+9
source

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 checking to make sure it is zero. You also had a problem using = instead of == - the last tests for equality, the first assigns a value.

EDIT: Edited according to Steve's comment below.

+4
source

Use the modulo function.

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

Please note that this does not take into account the leap of 100 and 400 years.

The correct code will be something like

 if(((year%4) == 0) && (((year%100)!=0) || ((year%400) == 0)) { //leap year } 
+3
source

In accordance with some rules that determine whether a year is a jump or not, the 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;} 
+1
source

Source: https://habr.com/ru/post/926254/


All Articles