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

I believe this is also correct:

int main() {
  
  int year;
std::cout << "Please enter the year: ";
std::cin >> year;

  if(year > 999 && year < 10000 ){
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
      std::cout << "This is a leap year";
    }else{
      std::cout << "This is not a leap year";
    }
  }else{
    std::cout << "Invalid year";
  }
  
}```
1 Like

Hey,
what I do not understand in this example is, why there is an “or”?
It says that there are 3 criteria, in my own words (as i understood them):

  1. year has to be evenly divided by 4
  2. year cannot be evenly divided by 100
  3. year can be evenly divided by 400
    So if it says there are 3 criteria that have to be met - in my understanding, criteria 1 and 2 and 3 have to be met.
    But why is the code like “1 and 2 or 3”?
    I really didn’t get that…
    Greetings,
    Michael

ok, after reading it again and again: could it be that:
In any case, the year has to be evenly divisable by 4, but:
it could either be not evenly divisable by 100 OR it could be evenly divisable by 400? This would explain the OR instead of the AND.

But should the wording than rather be: There are three “parameters” to be taken into account and 2 criteria should be fullfilled - because its either the divisable by 100 or the divisable by 400 rule…
so in the end it’s only 2 criteria that should be met…

I think you should take a step back, and first understand the requirements for when a year is a leap year. That should help determining the kind of logical operator you need

don’t know how common this knowledge is, but when dealing with whole centuries (1500, 1600, 1700, 1800, 1900, 2000, 2100) there is an exception to the divisible by four rule

only when the century is divisively by 400 is the century a leap year. so 1600 and 2000 are leap years, while 1700, 1800, 1900 are not

The algorithm the exercise decides to use determines with this edge case is to check if divisible by 4 and not divisible by 100 (not a century), or divisible 400.

This is something we developers have to deal with a lot, understanding the real life scenarios and then expressing them into code.

Wow, thanks for your fast answer and the explanation!

For me, it just made sense to break down the instructions but that turned out to be not a good idea as then the wording could be understood in different ways (at least in my opinion, but my mother-tongue is not english…) - but now, I think I understood it. Your explanation is more or less confirming the assumptions that I have come to meanwhile.

Thanks and have a nice day,
Michael

Letting go of the context of this exercise, its generally then a good idea to first understand what the requirements are. So let go of the code, and first understand it from a human perspective.

then go about fitting the code around that

programming is about translating requirements into code, but to do so, you need to understand those first. Important skill to have :slight_smile:

I will keep that in mind :slight_smile:
all the best, Michael

1 Like