### Trying to improve the leap year algorithm

I am trying to improve the leap year algorithm to tell me when the next leap year is if the year entered was not a leap year. I am still new to c++ and this is what I have so far.

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 #include // Access output stream using namespace std; // Access cout, endl, cin bool IsLeapYear(int); // Prototype for subalgorithm bool LeapYear1(int); bool LeapYear2(int); bool LeapYear3(int); bool LeapYear4(int); int main() { int year; // Year to be tested cout << "Enter a year AD, for example, 1997." << endl; // Prompt for input cin >> year; // Read year if (IsLeapYear(year)) // Test for leap year cout << year << " is a leap year." << endl; else cout << year << " is not a leap year." << endl; if (LeapYear1(year)) // Test for leap year cout << year << " is the next leap year." << endl; else if (LeapYear2(year)) cout << year << " is the next leap year." << endl; else if (LeapYear3(year)) cout << year << " is the next leap year."; else if (LeapYear4(year)) cout << year << " is the next leap year."; system("pause"); return 0; } //****************************************************** bool IsLeapYear(int year) // IsLeapYear returns true if year is a leap year and // false otherwise { if (year % 4 != 0) // Is year not divisible by 4? return false; // If so, can't be a leap year else if (year % 100 != 0) // Is year not a multiple of 100? return true; // If so, is a leap year else if (year % 400 != 0) // Is year not a multiple of 400? return false; // If so, then is not a leap year else return true; // Is a leap year } bool LeapYear1(int year) { if ((year + 1) % 4 != 0) return false; else if ((year + 1) % 100 != 0) return true; else if ((year + 1) % 100 != 0) return false; else return true; } bool LeapYear2(int year) { if ((year + 2) % 4 != 0) return false; else if ((year + 2) % 100 != 0) return true; else if ((year + 2) % 100 != 0) return false; else return true; } bool LeapYear3(int year) { if ((year + 3) % 4 != 0) return false; else if ((year + 3) % 100 != 0) return true; else if ((year + 3) % 100 != 0) return false; else return true; } bool LeapYear4(int year) { if ((year + 4) % 4 != 0) return false; else if ((year + 4) % 100 != 0) return true; else if ((year + 4) % 100 != 0) return false; else return true; return 0; }
Last edited on
Please edit your post to include [code][/code] tags around the code (the <> format).
I fixed it sorry.
 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 #include const int GREGORIAN_START = 1583 ; // first full year of the gregorian calendar bool IsLeapYear(int year) // IsLeapYear returns true if year is a leap year and // false otherwise { if( year < GREGORIAN_START ) return false ; // year before gregorian calendar if (year % 4 != 0) // Is year not divisible by 4? return false; // If so, can't be a leap year else if (year % 100 != 0) // Is year not a multiple of 100? return true; // If so, is a leap year else if (year % 400 != 0) // Is year not a multiple of 400? return false; // If so, then is not a leap year else return true; // Is a leap year } int NextLeapYear( int year ) { if( year < GREGORIAN_START ) year = GREGORIAN_START ; // increment year till we get to the next leap year (brute force) do ++year ; while( !IsLeapYear(year) ) ; return year ; } int main() { int year; // Year to be tested std::cout << "Enter a year AD, for example, 1997: " ; std::cin >> year; // Read year if( IsLeapYear(year) ) std::cout << year << " is a leap year.\n" ; else std::cout << year << " is not a leap year.\n" ; std::cout << "the next four leap years are: " ; for( int i = 0 ; i < 4 ; ++i ) { year = NextLeapYear(year) ; std::cout << year << ' ' ; } std::cout << '\n' ; }
Last edited on
 12345678910111213141516171819202122 #include bool IsLeapYear(int y) { return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); } int main() { int year; std::cout << "Enter a year: "; std::cin >> year; if (IsLeapYear(year)) std::cout << year << " is a leap year.\n"; else { std::cout << year << " is not a leap year.\n"; while (!IsLeapYear(++year)) ; std::cout << year << " is the next leap year.\n"; } }

Last edited on
Topic archived. No new replies allowed.