Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to get the number of days in a given month of a given year with C

1 Answer

0 votes
#include <stdio.h>
#include <stdbool.h>

bool isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int daysInMonth(int year, int month) {
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    if (month == 2 && isLeapYear(year)) {
        return 29;
    }
    
    return days[month - 1];
}

int main() {
    int year = 2024;
    int month = 2;

    printf("Days in month: %d\n", daysInMonth(year, month));
    printf("Days in month: %d\n", daysInMonth(2025, 1));
    printf("Days in month: %d\n", daysInMonth(2025, 2));
    printf("Days in month: %d\n", daysInMonth(2025, 4));

    return 0;
}


  
/*
run:
  
Days in month: 29
Days in month: 31
Days in month: 28
Days in month: 30
  
*/

 



answered Feb 19, 2025 by avibootz
...