186k views
0 votes
write a function that uses the switch structure to compute the number of days in a year up to a given date, given the year, the month, and the day of the month. Test your function for the year 2020, the month of March (3), and the day 23.

User Anamika
by
7.4k points

1 Answer

6 votes

Final answer:

To compute the number of days in a year up to a given date, you can write a function using the switch structure. Take into account the leap year rule when calculating the number of days for each month.

Step-by-step explanation:

To write a function that uses the switch structure to compute the number of days in a year up to a given date, you can use the following steps:

  1. Define the year, month, and day as variables.
  2. Create a switch statement for the month value.
  3. For each month case, return the total days up to the given day using if statements and the leap year rule.
  4. Handle the case for February separately, considering the leap year rule where February has 29 days in leap years.
  5. For the remaining months, return the total days based on their respective values.
  6. Finally, return the total number of days for the given date in the year.

For example, for the year 2020, month 3 (March), and day 23, the function would return 83, as there are 31 days in January, 29 days in February (leap year), and 23 days in March.

User Nikamanish
by
8.0k points