Kodewick
Start learning →
Basic←→0 / 50

Day of the year

#028Basicyear % 100 != 08 min

Write day_of_year(year, month, day) to work out which day of the year it is. March 1, 2023 is day 60 (31 + 28 + 1). In a leap year February has 29 days, so March 1, 2024 is day 61. days holds the days in each month of a normal year, and days[0] is January.

Examples
day_of_year(2024, 3, 1)→61
day_of_year(2023, 3, 1)→60
day_of_year(2025, 12, 31)→365
The functionint day_of_year(int year, int month, int day)
main.c
int day_of_year(int year, int month, int day) {
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    // your code here: if it is a leap year, make February 29 days
    int total = day;
    for (int m = 1; m < month; m++) {
        // your code here: add the days of month m to total
    }
    return total;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.