Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Exercise3
- {
- class Program
- {
- static void Main(string[] args)
- {
- string inputString;
- int year = 0;
- int month = 0;
- int num = 1;
- int firstDay = 0;
- bool validYear = true;
- bool validMonth = true;
- string[] monthString = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- int[,] calendar = new int[6, 7];
- Console.WriteLine("Program to print a calendar for a specified month in a specified year entered");
- Console.WriteLine();
- // Getting the chosen Year
- while (validYear)
- {
- Console.Write("Enter the year: ");
- inputString = Console.ReadLine();
- if (int.TryParse(inputString, out year))
- {
- validYear = false;
- }
- else
- {
- Console.WriteLine();
- Console.WriteLine("You have entered an invalid value");
- validYear = true;
- }
- }
- // Getting the chosen Month
- while (validMonth)
- {
- Console.Write("Enter the number of the month (Jan = 1, etc): ");
- inputString = Console.ReadLine();
- if (int.TryParse(inputString, out month))
- {
- if (month < 1 || month > 12)
- {
- Console.WriteLine();
- Console.WriteLine("Number entered must be between 1 and 12");
- validMonth = true;
- }
- else
- {
- validMonth = false;
- }
- }
- else
- {
- Console.WriteLine();
- Console.WriteLine("You have entered an invalid value");
- validMonth = true;
- }
- }
- //DateTime for first day of Month and the amount of days in month
- DateTime dateGiven = new DateTime(year, month, 1);
- firstDay = (int)dateGiven.DayOfWeek - 1;
- if (firstDay == -1)
- {
- firstDay = 6;
- }
- int daysInMonth = System.DateTime.DaysInMonth(year, month);
- // Calculating
- for (int rows = 0; rows <= 5; rows++)
- {
- if (rows == 0)
- {
- for (int columns = firstDay; columns <= 6; columns++)
- {
- calendar[rows, columns] = num++;
- }
- }
- if (rows > 0 && rows <= 5)
- {
- for (int columns = 0; columns <= 6; columns++)
- {
- if (num == daysInMonth + 1)
- {
- columns = 6;
- rows = 5;
- }
- else
- {
- calendar[rows, columns] = num++;
- }
- }
- }
- }
- // Display
- Console.WriteLine();
- Console.WriteLine(" " + monthString[month - 1] + " ");
- Console.WriteLine(" Mo Tu We Th Fr Sa Su ");
- for (int rows = 0; rows <= 5; rows++)
- {
- for (int columns = 0; columns <= 6; columns++)
- {
- if (calendar[rows, columns] < 10)
- {
- Console.Write(" ");
- }
- else
- {
- Console.Write(" ");
- }
- if (calendar[rows, columns] == 0)
- {
- Console.Write(" ");
- }
- else
- {
- Console.Write(calendar[rows, columns]);
- Console.Write(" ");
- }
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement