w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Number of days in the specified month and year

Secondary Nav

C# Sharp DateTime : Exercise-19 with Solution

Write a C# Sharp program to get the number of days of the specified month and year.

Expected Output :

31                                                                               
28                                                                               
29  

C# Sharp Code:

using System;
public class Example19
{
	static void Main()
	{
		const int July = 7;
		const int Feb = 2;

		int daysInJuly = System.DateTime.DaysInMonth(2016, July);
        Console.WriteLine(daysInJuly);

		// 2013 was not a leap year.
		int daysInFeb = System.DateTime.DaysInMonth(2013, Feb);
        Console.WriteLine(daysInFeb);

		// 2004 was a leap year.
		int daysInFebLeap = System.DateTime.DaysInMonth(2004, Feb);
		Console.WriteLine(daysInFebLeap);
	}
}

C# Sharp Practice online:

using System;
public class Example19
{
	static void Main()
	{
		const int July = 7;
		const int Feb = 2;

		int daysInJuly = System.DateTime.DaysInMonth(2016, July);
        Console.WriteLine(daysInJuly);

		// 2013 was not a leap year.
		int daysInFeb = System.DateTime.DaysInMonth(2013, Feb);
        Console.WriteLine(daysInFeb);

		// 2004 was a leap year.
		int daysInFebLeap = System.DateTime.DaysInMonth(2004, Feb);
		Console.WriteLine(daysInFebLeap);
	}
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number