Home » C++ programs » C++ Most popular & searched programs

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.

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.

Consider the program:



#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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.