C++ program to find total number of days in given month of year

C++ program to find/print total number of days in a month, in this program, we will input month, year and program will print total number of days in that month, year.
[Last updated : February 26, 2023]

Finding the total number of days in given month of year

While writing code in C++ programming language, sometimes we need to work with the date, this program may useful for you.

In this program, we are going to find total number of days in a month, year. Here, we are designing a function named getNumberOfDays(), function will take month and year as arguments, function will return total number of days.

C++ code to find the total number of days in given month of year

#include <iostream>
using namespace std;

// function will return total number of days
int getNumberOfDays(int month, int year)
{
    // leap year condition, if month is 2
    if (month == 2) {
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
            return 29;
        else
            return 28;
    }
    // months which has 31 days
    else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
        || month == 10 || month == 12)
        return 31;
    else
        return 30;
}

// Main program
int main()
{
    int days = 0;

    days = getNumberOfDays(2, 2016);

    cout << endl
         << "Number of Days :  " << days;

    return 0;
}

Output

Number of Days:  29


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.