FAQ: Logical Operators - Review

This was my solution, year limit or anything.

#include

int year;

int main() {

std::cout << "Enter the year. ";

std::cin >> year;

if (year%4==0 and (year%100!=0 or year%400==0)) {

std::cout << year << " is a leap year.\n";

}

else {

std::cout << year << " is not a leap year.\n";

}

}

Hello everyone,
I have few questions on this leap year problem. The first one is about the variable “y”. Why do we declare the variable y as 0? Can we declare it as 1 or just int y;

The second question is regarding % sign. Is this the sign used for division in c++ or is it a mathematical step for this problem?

I used else and else if parameter as well but the solution shows a very neat code with just one if condition of division. How do we distinguish the difference?

Thanks.

all think all those are possible, we just need a variable to store the user input (cin).

The division operator is a forward slash (/), % is the modulo operator, which gives you the remainder of the division.

you can google for an explanation on the modulo operator if you need it

your code has several flaws, Firstly, you use division operator. Secondly, not all years which are divisible by 4 are leap years. For centuries (1500, 1600, 1700 and so forth) there is a special rule. Only if the century is divisible by 400 is it a leap year. so 1600 and 2000 are leap years, while 1700, 1800 en 1900 are not

1 Like

So I did this, which I feel is rather skirting the issue because I didn’t need to use any of the logical operators. It was just a question of putting the conditions in an order that worked. Still, it does work…

Hello all. I have a quick question. I tried to complete the challenge at the end of this lesson, and for some reason I thought it would be a good idea to store the values of the modulo operations in Integers. Why I thought this was a good idea? no clue, it’s obviously cleaner to just do it the way they do in the answer/hint. However what I can’t quite figure is why the result of the modulo op doesn’t appear to store in the Int the way I would have assumed it would. As you can see from my screenshot, no matter what year I input the ‘int remainder’ is valued at 0. If I keep everything the same and just put the modulo operations in the ‘else if’ it works exactly as it should. Any idea what I’m missing here, please be gentle I’m new. Regards,

Aaron

If anyone happens across this post in the future, I should let you know I figured out my mistake. All I can say is never forget that a program executes code from Top to Bottom. Making sure the initialization of the variables was moved below the line

std::cin >> year;

completely solved the problem. As I had it here, the modulo operation was running before the value for the variable ‘year’ had been inputted. Cheers!

Aaron

1 Like

Ok thanks for letting us know.

Here is a great site for explaining the wording much easier. I didn’t truly understand the y % 100 != 0 until I read it here.

https://leap-years.calculators.ro/is-this-a-leap-year-or-a-common.php?year=1600&year_type=leap_year&last-leap-year-before-1600=1596&next-leap-year-after-1600=1604