Answer to Question #190523 in C# for Nkhululeko Chabala

Question #190523

Write a program that reads in a month of the year as a number (eg 1 for January, 4 for April) and then displays the number of days in that month. Keep in mind the impact of leap years. Note – Only for February the actual year is important (you should ask the user for the year of interest)


1
Expert's answer
2021-05-09T05:36:52-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        static void Main(string[] args)
        {
            int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            string []monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
            Console.Write("Enter the year as a number [1-999999]: ");
            int year = int.Parse(Console.ReadLine());
            Console.Write("Enter the month of the year as a number [1-12]: ");
            int month = int.Parse(Console.ReadLine());
            if (month < 1 || month > 12)
            {
                Console.WriteLine("\nWrong month.\n");
            }
            else {
                if (year % 4 == 0 && month == 2)
                {
                    Console.WriteLine("\nThe year is leap. The number of days in {0} month is: {1}\n", monthNames[month - 1], (days[month - 1] + 1));
                }
                else {
                    Console.WriteLine("\nThe number of days in {0} month is: {1}\n", monthNames[month - 1], days[month - 1]);
                }
            }
            Console.ReadLine();
        }
    }
}







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
APPROVED BY CLIENTS