DSA Factory
Free lessonsProgramming basics · Stage 2 · Making decisions · Step 3

And, or, not

Combine several conditions into one answer. About 9 minutes.

Two questions at once

A fairground ride says: you must be tall enough and have a ticket. Both must be true to get on. That joining word is and.

Sometimes one is enough. A shop gives a discount to students or to people over 60. That joining word is or: the answer is true when at least one of the questions is true. Both being true also counts.

And: needs every part true.
Or: needs at least one part true.
In code
can_ride = tall and has_ticket
discount = student or senior

Not, and brackets

The word not flips an answer: true becomes false, and false becomes true. Use it for rules like "divisible by 4, but not by 100".

When you mix and with or, use brackets to show what goes with what, just like in arithmetic. The leap year rule is a good example: a year is a leap year if it divides by 4 but not by 100, or if it divides by 400.

Mixing and with or? Use brackets to make the grouping clear.
In code
by4 = year % 4 == 0
by100 = year % 100 == 0
by400 = year % 400 == 0
leap = (by4 and not by100) or by400
Is 1900 a leap year?
year: 1900
0
by4: True
1
by100: ?
2
by400: ?
3
leap: ?
4
line
by4
True

1900 divides evenly by 4, so by4 is true.

Move 1 of 4
Quick check

Is the year 2000 a leap year, by the rule: divisible by 4 and not by 100, or divisible by 400?

  1. AYes
  2. BNo, because it divides by 100
  3. CNo, because it isn't divisible by 4
Show the answer

Yes. 2000 divides by 100, so the first part fails. But it divides by 400, so the second part is true.

Your problem

Leap year

A year is a leap year when it is divisible by 4 but not by 100, or when it is divisible by 400. Return true if the year is a leap year.

Example
year = 2024 → true

1 ≤ year ≤ 9,999

Solve it in your browserPython, C++, Java or JavaScript. Hints if you get stuck. No sign-up needed.
Start coding