// Accepts a year
// Returns 0 if it is NOT a leap year
// Returns 1 if it IS a leap year
// Returns 2 if it is out of bounds
int is_leapYear(int year)
{
if (year < 1000) return 2;
if (year > 9999) return 2;
if (year%4) return 0; // if it is NOT a multiple of 4, it is NOT a leap year
if (year%100 == 0 && year%400!=0) return 0; // if it IS a multiple of 100 and NOT a multiple of 400 it is NOT a leap year
return 1; // otherwise it IS a leap year
}
#include <iostream>
usingnamespace std;
int leapyear (int yr)
{
if ((yr % 4 == 0) && !(yr % 100 == 0))
return 1;
elseif (yr % 400 == 0)
return 1;
return 0;
}
int main()
{
constint arraySize = 4;
int year[ arraySize ];
cout << "Enter " << arraySize << " four digits years:\n";
for ( int i = 0; i < arraySize; i++ )
{
cin >> year[ i ];
if (year[i] > 9999 || year[i] < 1000)
{
cout <<"That was an incorrect year, make sure it is only 4 digits long" <<endl;
--i;
}
}
cout <<endl;
for (int r = 0; r < arraySize; ++r)
if (leapyear(year[r]))
cout << year[r] <<" is a leap year.\n";
return 0;
}
$ ./File
Enter 4 four digits years:
999
That was an incorrect year, make sure it is only 4 digits long
2348
2010
10000
That was an incorrect year, make sure it is only 4 digits long
4000
7675
2348 is a leap year.
4000 is a leap year.
for ( int i = 0; i < arraySize; i++ )
{
cin >> year[ i ];
if (year[i] > 9999 || year[i] < 1000)
{
cout <<"That was an incorrect year, make sure it is only 4 digits long" <<endl;
--i;
}
}
I think that in the original post the array was defined that each its element would contain only one digit. It was not defined to contain four years.:)