Can someone help me with this question?

The twenty-first century began on 1 January 2001 (a Monday) and will end on 31 December 2100 (a Friday). What percentage of twenty-first century Wednesdays fall on the last day of a month?

What’s your hypothesis as a starting point to tackle this? We can help you develop it.

I’d figure out how to go through each day of the month for the years. then figure out how to keep track of the day of the week for each of those days. then keep track of how many of those are wednesdays. then see how many of those are wednesdays AND the end of the month

1 Like

Just an idea, seems like a great opportunity to put the modulo operator % to use:

#include <iostream>

int main() {

  for (int i = 0; i < 20; i++) {

    if (i % 5 == 0) {

      std::cout << i << " is perfectly divisible by 5\n";
1 Like

The issue is how to account for the leap years as well :thinking:

Do you know the solution?

Yes I do (I was being rhetorical in my last reply). The policy of the forum is to help out and not give solutions though. But we’re free to walk through as many steps as needed. This type of problem is essentially conceptual at its core, the coding techniques are fairly simple. Hence, once we clarify the intent, the code should flow much easier.

Then, even once the problem is done, questions remain like factoring and such. Ideally, this code should work on any range of years. Even more ideally, it should take abnormal year cycles (like the ones in another planet, if scientists need the code). All of which of course is not too bad once the core ideas are clarified.

1 Like

I just need the solution.

As @toastedpitabread, has said, we cannot give you the solution. The question sounds like one asked for some type of school assignment. If that’s the case, giving you the solution, would amount to cheating which is not tolerated on the forums.

One thing that may help is making the problem smaller. Rather than figuring out how to count months that end on a Wednesday for a period of a hundred years, come up first with a way to determine if a month ends on a Wednesday. Start with January, 2001. Then figure out how many months in 2001 ended on a Wednesday. Then test it over a period you can still double check manually like maybe 4 years (to include a leap year). Once that works, you should have a solution that will work for the 100 year period in question. You can come to the forums with questions along the way, but we cannot just give you a solution.

1 Like