Write a program that reads in a month of the year as a number and display the number of days in that mind. Keep in mind the impact of leap years
using System;
namespace MyApplication
{
class Program
{
static int findNumberOfDays(int month, int year)
{
if (month < 1 || month > 12)
{
return -1;
}
if (month == 2)
{
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
return 29;
}
else
{
return 28;
}
}
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
else
{
return 30;
}
}
static void Main(string[] args)
{
int monthAsNumber;
int year;
int numberOfDays;
Console.WriteLine("Please enter the month of the year as a number: ");
monthAsNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter the year: ");
year = Convert.ToInt32(Console.ReadLine());
numberOfDays = findNumberOfDays(monthAsNumber, year);
if (numberOfDays == -1)
{
Console.WriteLine("\nError: You entered a wrong month number, please try again!");
} else
{
Console.WriteLine("\nThe number of days in your month is: " + numberOfDays);
}
}
}
}
Comments
Leave a comment