Count the Number of Days in a Given Month of a Year in C++



There are a total of 365 days in a normal year and 366 days in a leap year. In this article, we are going to learn how we can find the number of days in a month of a given particular year. Here, we will learn about different approaches for calculating the number of days in a given month using C++.

How to Find the Number of Days in a Given Month?

There are a total of 12 months in a year. The number of days in a given month from 1 to 12 in a given leap or non-leap year. The number of days in each month is as follows:

January: 31 days
February: 28 on non-leap year and 29 days on leap year
March: 31 days
April: 30 days
May: 31 days
June: 30 days
July: 31 days
August: 31 days
September: 30 days
October: 31 days
November: 30 days
December: 31 days

The condition for a year to be a leap year is given below:

  • If it is divisible by 4 and not divisible by 100, then it is a leap year.
  • If it is divisible by 400, then it is a leap year.

For example:
2000 is a leap year as it is divisible by 400.
2024 is a leap year as it is divisible by 4 and not by 100.
2100 is not a leap year as it is divisible by 100 but not by 400.

Different Approaches to Find the Number of Days in a Month

Using a Conditional Statement

This is the simplest approach. In this approach, we use a simple conditional statement to calculate the number of days in a given month and given year. We check if the month is February and whether the year is a leap year or not.

Steps for Implementation:

  • Take input for the month and year.
  • Use a switch-case or if-else statement to determine the number of days.
  • Print the result.

Implementation Code:

#include<bits/stdc++.h>
using namespace std;

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

int getDaysInMonth(int month, int year) {
    switch (month) {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
            return 31;
        case 4: case 6: case 9: case 11:
            return 30;
        case 2:
            return isLeapYear(year) ? 29 : 28;
        default:
            return -1; // Invalid month
    }
}

int main() {
    int month, year;
    cout << "Enter month (1-12): ";
    cin >> month;
    cout << "Enter year: ";
    cin >> year;
    
    int days = getDaysInMonth(month, year);
    if (days == -1)
        cout << "Invalid month entered!";
    else
        cout << "Number of days: " << days;
    
    return 0;
}

Output:

Enter month (1-12): 2
Enter year: 2024
Number of days: 29

Time Complexity: O(1)
Space Complexity: O(1)

Using an Array for Days in Each Month

In this approach, we store the number of days each month in an array. This will make the program more efficient and readable.

Steps for Implementation:

  • First, create an array containing the number of days for each month.
  • Now, use the month index to access the corresponding value.
  • Check if the year is a leap year when the month is February.

Implementation Code:

#include<bits/stdc++.h>
using namespace std;

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

int main() {
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int month, year;
    
    cout << "Enter month (1-12): ";
    cin >> month;
    cout << "Enter year: ";
    cin >> year;
    
    if (month < 1 || month > 12) {
        cout << "Invalid month entered!";
        return 0;
    }
    
    if (month == 2 && isLeapYear(year))
        cout << "Number of days: 29";
    else
        cout << "Number of days: " << daysInMonth[month - 1];
    
    return 0;
}

Output:

Enter month (1-12): 2
Enter year: 2023
Number of days: 28

Time Complexity: O(1)
Space Complexity: O(1)

Updated on: 2025-03-27T14:35:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements