In: Computer Science
Write a program named CheckMonth2 that prompts a user to enter a birth month and day.
Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February).
If the month and day are valid, display them with a message. For
example, if the month entered is 2, and the day entered is 17, the
output should be 2/17 is a valid birthday.
This is the code i start with. (Answer is C# Please.)
using System;
using static System.Console;
class CheckMonth2
{
static void Main()
{
// Write your main here
}
}
using System;
class CheckMonth2{
public static void Main (string[] args) {
Console.WriteLine ("Enter day and month : ");
int day = Convert.ToInt32(Console.ReadLine());
int month = Convert.ToInt32(Console.ReadLine());
int days=getDays(month);
if(days==-1){
Console.WriteLine("Invalid month. Must be 1 to 12");
}
else if(day!=days){
Console.WriteLine("Invalid days. must be "+days);
}
else{
Console.WriteLine(month+"/"+day+" is a valid birthday");
}
}
static int getDays(int mm) {
int res = -1;
switch (mm) {
case 1:
res = 31;
break;
case 2:
res = 29;
break;
case 3:
res = 31;
break;
case 4:
res = 30;
break;
case 5:
res = 31;
break;
case 6:
res = 30;
break;
case 7:
res = 31;
break;
case 8:
res = 31;
break;
case 9:
res = 30;
break;
case 10:
res = 31;
break;
case 11:
res = 30;
break;
case 12:
res = 31;
break;
}
return res;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me