Hey, I’ve been interested in C++ for a long time now and started using this service to learn it but now I’m stuck on this exercise. Not sure if my math is wrong or the output is . (I also used this resource to further assist me on the math, https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year ) Thanks to anyone who’s willing to help. Edit: I added the second else if just for “making sure” purposes although It’s probably unnecessary.
Here’s code:
#include <iostream>
int main() {
// Variables
int y;
// Store input Data
std::cout << "Enter year: ";
std::cin >> y;
// Calculation
if (y < 1000 or y > 9999) {
// Testifies if year is four-digit
std::cout << "Invalid Year.\n";
} else if (y / 4 == 0 and y / 100 == 0 and y / 400 == 0) {
// if y is divisible by 4, 100 and 400, then it's a year leap.
std::cout << y;
std::cout << " falls on a leap year.\n";
} else if (y / 4 == 0 and y / 100 != 0) {
// if y is divisble by 4 and not 100 then it's a year leap.
std::cout << y;
std::cout << " falls on a leap year.\n";
} else {
// else it isn't a year leap.
std::cout << y;
std::cout << " isn't a leap year.\n";
}
}