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.
#include <iostream> // Access output stream
usingnamespace 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;
elseif (LeapYear2(year))
cout << year << " is the next leap year." << endl;
elseif (LeapYear3(year))
cout << year << " is the next leap year.";
elseif (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?
returnfalse; // If so, can't be a leap year
elseif (year % 100 != 0) // Is year not a multiple of 100?
returntrue; // If so, is a leap year
elseif (year % 400 != 0) // Is year not a multiple of 400?
returnfalse; // If so, then is not a leap year
elsereturntrue; // Is a leap year
}
bool LeapYear1(int year)
{
if ((year + 1) % 4 != 0)
returnfalse;
elseif ((year + 1) % 100 != 0)
returntrue;
elseif ((year + 1) % 100 != 0)
returnfalse;
elsereturntrue;
}
bool LeapYear2(int year)
{
if ((year + 2) % 4 != 0)
returnfalse;
elseif ((year + 2) % 100 != 0)
returntrue;
elseif ((year + 2) % 100 != 0)
returnfalse;
elsereturntrue;
}
bool LeapYear3(int year)
{
if ((year + 3) % 4 != 0)
returnfalse;
elseif ((year + 3) % 100 != 0)
returntrue;
elseif ((year + 3) % 100 != 0)
returnfalse;
elsereturntrue;
}
bool LeapYear4(int year)
{
if ((year + 4) % 4 != 0)
returnfalse;
elseif ((year + 4) % 100 != 0)
returntrue;
elseif ((year + 4) % 100 != 0)
returnfalse;
elsereturntrue;
return 0;
}
#include <iostream>
constint 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 ) returnfalse ; // year before gregorian calendar
if (year % 4 != 0) // Is year not divisible by 4?
returnfalse; // If so, can't be a leap year
elseif (year % 100 != 0) // Is year not a multiple of 100?
returntrue; // If so, is a leap year
elseif (year % 400 != 0) // Is year not a multiple of 400?
returnfalse; // If so, then is not a leap year
elsereturntrue; // 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' ;
}
#include <iostream>
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";
}
}