there is something fishy with your math.
its probably working ok, but
if a year is divisible by 400, it is ALSO divisible by 4 and 100. the 400 check is redundant
as is the 100. If a year is divisible by 100, it is also divisible by 4.
therefore %4 is sufficient and the other 2 are not needed at all.
and clutter. Those if / else blocks are identical. If you combined the conditions, you can do the block once only, just one for is a leap year and one for is not. (this assumes all the conditions are useful, which as noted above, they are redundant, the point is that if you run into this again where the code is the same for a couple of conditions, combine the conditions).
Some say "Leap Year" and some say "Not a leap year", but yeah the last two lines are identical in each block and could be put after the if-else chain to avoid code repetition.
if a year is divisible by 400, it is ALSO divisible by 4 and 100. the 400 check is redundant
as is the 100. If a year is divisible by 100, it is also divisible by 4.
therefore %4 is sufficient and the other 2 are not needed at all.
It's possible to write it more compactly but the checks are not redundant.
Leap years are years that are evenly divisible by 4 except for years that are evenly divisible by 100 but not 400.
#include <iostream>
int main()
{
char again { 'Y' };
do
{
int year { };
while (std::cout << "Enter a year: " && !(std::cin >> year))
{
std::cout << "Wrong Data. Try again.\n\n";
std::cin.clear();
std::cin.ignore(100, '\n');
}
// if year is divisible by 4 AND not divisible by 100
// OR if year is divisible by 400
// then it is a leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
std::cout << year << " is a leap year.\n\n";
}
else
{
std::cout << year << " is not a leap year.\n\n";
}
std::cout << "Try Again [Y/N]? ";
std::cin >> again;
std::cout << '\n';
}
while (again == 'Y' || again == 'y');
}
Enter a year: 1900
1900 is not a leap year.
Try Again [Y/N]? y
Enter a year: blarf
Wrong Data. Try again.
Enter a year: 2022
2022 is not a leap year.
Try Again [Y/N]? y
Enter a year: 2020
2020 is a leap year.
Try Again [Y/N]? n