Answer to Question #62278 in C# for Peter

Question #62278
Create an app that asks the user to enter a number that represents a month of the year and also asks them to enter a year. When entered, the app should then display the month and say if their chosen year is a leap year or not.

Example: If the user inputs the number 2, it should then Display February
1
Expert's answer
2016-09-29T14:29:04-0400
using System;
using System.Globalization;

namespace DateApp
{
class Program
{
static int CheckInput(string str, int lowerLim, int upperLim)
{
int dateVal;
string date;
do
{
Console.WriteLine(str);
date = Console.ReadLine();
} while (!int.TryParse(date, out dateVal) || Convert.ToInt32(date) < lowerLim || Convert.ToInt32(date) > upperLim);
return dateVal;
}
static void Main(string[] args)
{
string monthMessage = "Type a month number [1-12]:",
yearMessage = "Type year [1000-9999]:";
int month = CheckInput(monthMessage, 1, 12),
year = CheckInput(yearMessage, 1000, 9999);
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month);
Console.WriteLine("\nRESULTS:\nMonth: {0}", monthName);
Console.WriteLine("Year {0} is {1} leap", year, DateTime.IsLeapYear(year) ? "" : "not");
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS