There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
If the year can be evenly divided by 4 then it is a leap year, however…
If that year can be evenly divided by 100, and it is not evenly divided by 400, then it is NOT a leap year.
you should really read them together, the however on the previous line is crucial there.
lets explain this one first from a human perspective, then we will get back to the code in a second. A little know fact about leap years, is that for centuries (1500, 1600, 1700, 1800, 1900, 2000) and so forth, there is a special rule. A century is only a leap year when divisible by 400. So 1600 and 2000 are leap years, but 1700, 1800 and 1900 are not
Then its good to realize, that and (&&) operator is evaluated before the or (||) operator.
So the best way for to express this in code, is to check if a year is divisible by 4 but not divisible by 100. This way, we deal with 1700, 1800 and 1900 which are not leap years.
so for 2004 we get:
2004 % 4 == 0 && 2004 % 100 != 0
we get:
0 == 0 && 4 != 0
so we are good.
lets also do 1900:
1900 % 4 == 0 && 1900 % 100 != 0
results in:
0 == 0 && 0 != 0
then we get:
true && false
which is false, which is good. 1900 is not a leap year
then finally we have % 400 to deal with 1600, 2000 and so forth
…you know how there are orders of operation in regular math? (I mean Bedmas) This order can change the answer to the expression: 5 + 5 / 5 there are two scenarios;
a) ignoring Bedmas --> 2
b) Bedmas --> 6
Of course in proper mathematics, the answer is always going to be b)
My question is,
a) are there similar rules in coding with the and and or conditions
b) Can I use brackets?
For example, if (200 > 300 && 200 > 100 || 200 > 99)
of, further simplified if (false && true || true)
Would this expression evaluate to true and have the code run, or would it evaluate to false?
Are we looking at it like this:
if (false && true)
or
if (false || true)
As for my second question,
as in Bedmas, could I use brackets?
for example, could I do something like
if (false && (true || true))
(which would evaluate to false)
Please reply, I feel this information would be very important, of course I could just experemint on my own, but I hope someone will reply
hey guys
please i need assistance with the hint, why did they write
int y = 0
like is it necessary can`t i just write int y ?
and also it brings me to this part with is kind of confusing, like mathematically i don´t think it´s right
else if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
It’s a good habit and in some languages its required to declare an initialize variables. You could but you might get unexpected behavior/results.
Order of operation still exist as well.
if Y divided into 4 has no remainder and y divided into 100 does not a remainder OR y divided into 400 has no remainder, is what it is saying. Whether that is expected results or not is another thing.
When you say “can be evenly divided by 100”, doesn’t that mean it should be exact? there should be no remainders, but on the answer it says “y % 100 != 0”
I found this website helpful in solving the leap-year exercise as the instructions were explained in more detail and steps through the process.
Thank you,
So for like 3 or 4 programs the bash will give me errors about something thats literally correct…i’ve even copyed the hint program and it is the same , what should i do ?? it is the problem from me or from the site??
hey guys, I happen to study functions and forgot about this exercise. So this is my Try, I wanted a function to count the number of digits the user had entered and if that digit is equal to 4 check if the year is a leap_year or no. about the instructions, they’re not clear so I found those instead: Return true if year is a multiple 0f 4 AND not multiple of 100 OR year is multiple of 400. I recommend to complete C++ Functions before going back and look at this code.This is an additional for y’all.
Sir, program is not working, compiling doesn’t show any error and it gets compiled properly but the program is not running it shows nothing? Just as it is? But it is running in code blocks?
The wording of the “however” was not precise or specific enough to me which is why I thought it meant 100==0 || 400==0. Your link shows that if “year is divisible by 100 then it is not a leap year”; the wording made things far more confusing, so thanks for clearing that up. Of course if you know how leap years work none of this is necessary.
it took me a while but i completed the code satisfactory and when i ran the program it asked me the question and when i input the year it would calculate and spit out the correct answer… the issue is that the website itself wont let me move forward from this point. i even went so far as to delete my code (which i was unhappy about because i felt an accomplishment for coming up with it) and putting in the “answer” on the “get unstuck” section. after doing this, the program runs the exact way i had it originally and the website still will not let me move forward… can someone help?