Answer to Question #191003 in C# for Wise Tshikovhela

Question #191003

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


1
Expert's answer
2021-05-09T10:39:57-0400
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);
            }
        }
    }
}

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