Answer to Question #284279 in C# for OraaKim

Question #284279

Create a program that will generate a month calendar for the year 2022.

Sample output:

Enter month in number (e.g., 3 for March): 4


Module 03 | Performance Tasks

Calendar

April 2022

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30


1
Expert's answer
2022-01-02T08:17:47-0500
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calendar
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter month in number (e.g., 3 for March): ");
            int month = Convert.ToInt32(Console.ReadLine());
            DateTime d0 = new DateTime(2022, month, 1);

            Console.WriteLine("Module 03 | Performance Tasks");
            Console.WriteLine("Calendar");
            Console.WriteLine("{0} 2022", d0.ToString("MMMM", CultureInfo.InvariantCulture));

            Console.WriteLine("Sun Mon Tue Wed Thu Fri Sat");
            
            int dcounter = 0;
            for (int j = 0; j < (int)d0.DayOfWeek; j++)
            {
                Console.Write("{0} ", "".PadLeft(3, ' '));
                dcounter++;
            }
            Console.Write("{0} ", d0.Day.ToString().PadLeft(3, ' '));
            dcounter++;

            if (dcounter % 7 == 0)
                Console.WriteLine();
            for (int i = 2; i <= DateTime.DaysInMonth(2022, month); i++)
            {
                DateTime d = new DateTime(2022, month, i);
                Console.Write("{0} ", i.ToString().PadLeft(3, ' '));
                dcounter++;

                if (dcounter % 7 == 0)
                    Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}

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