Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. namespace Exercise3
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string inputString;
  8. int year = 0;
  9. int month = 0;
  10. int num = 1;
  11. int firstDay = 0;
  12. bool validYear = true;
  13. bool validMonth = true;
  14. string[] monthString = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  15. int[,] calendar = new int[6, 7];
  16.  
  17. Console.WriteLine("Program to print a calendar for a specified month in a specified year entered");
  18. Console.WriteLine();
  19.  
  20. // Getting the chosen Year
  21. while (validYear)
  22. {
  23. Console.Write("Enter the year: ");
  24. inputString = Console.ReadLine();
  25. if (int.TryParse(inputString, out year))
  26. {
  27. validYear = false;
  28. }
  29. else
  30. {
  31. Console.WriteLine();
  32. Console.WriteLine("You have entered an invalid value");
  33. validYear = true;
  34. }
  35. }
  36.  
  37. // Getting the chosen Month
  38. while (validMonth)
  39. {
  40. Console.Write("Enter the number of the month (Jan = 1, etc): ");
  41. inputString = Console.ReadLine();
  42. if (int.TryParse(inputString, out month))
  43. {
  44. if (month < 1 || month > 12)
  45. {
  46. Console.WriteLine();
  47. Console.WriteLine("Number entered must be between 1 and 12");
  48. validMonth = true;
  49. }
  50. else
  51. {
  52. validMonth = false;
  53. }
  54. }
  55. else
  56. {
  57. Console.WriteLine();
  58. Console.WriteLine("You have entered an invalid value");
  59. validMonth = true;
  60. }
  61. }
  62.  
  63. //DateTime for first day of Month and the amount of days in month
  64. DateTime dateGiven = new DateTime(year, month, 1);
  65. firstDay = (int)dateGiven.DayOfWeek - 1;
  66. if (firstDay == -1)
  67. {
  68. firstDay = 6;
  69. }
  70. int daysInMonth = System.DateTime.DaysInMonth(year, month);
  71.  
  72. // Calculating
  73. for (int rows = 0; rows <= 5; rows++)
  74. {
  75. if (rows == 0)
  76. {
  77. for (int columns = firstDay; columns <= 6; columns++)
  78. {
  79. calendar[rows, columns] = num++;
  80. }
  81. }
  82. if (rows > 0 && rows <= 5)
  83. {
  84. for (int columns = 0; columns <= 6; columns++)
  85. {
  86. if (num == daysInMonth + 1)
  87. {
  88. columns = 6;
  89. rows = 5;
  90. }
  91. else
  92. {
  93. calendar[rows, columns] = num++;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. // Display
  100. Console.WriteLine();
  101. Console.WriteLine("           " + monthString[month - 1] + "          ");
  102. Console.WriteLine(" Mo  Tu  We  Th  Fr  Sa  Su ");
  103.  
  104. for (int rows = 0; rows <= 5; rows++)
  105. {
  106. for (int columns = 0; columns <= 6; columns++)
  107. {
  108. if (calendar[rows, columns] < 10)
  109. {
  110. Console.Write("  ");
  111. }
  112. else
  113. {
  114. Console.Write(" ");
  115. }
  116. if (calendar[rows, columns] == 0)
  117. {
  118. Console.Write(" ");
  119. }
  120. else
  121. {
  122. Console.Write(calendar[rows, columns]);
  123. Console.Write(" ");
  124. }
  125. }
  126. Console.WriteLine();
  127. }
  128. Console.WriteLine();
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement