In: Computer Science
Write a C# program that prints a calendar for a given year. Call
this program calendar. The program prompts the user for two
inputs:
1) The year for which you are
generating the calendar.
2) The day of the week that January
first is on, you will use the following notation to set the day of
the week:
0
Sunday
1
Monday
2
Tuesday
3 Wednesday
4
Thursday
5
Friday
6 Saturday
Your program should generate a calendar similar to the one shown in the example output below. The calendar should be printed on the screen. Your program should be able to handle leap years. A leap year is a year in which we have 366 days. That extra day comes at the end of February. Thus, a leap year has 366 days with 29 days in February. A century year is a leap year if it is divisible by 400. Other years divisible by 4 but not by 100 are also leap years.
Example: Year 2000 is a leap year because it is divisible by
400. Year 2004 is a leap year because it is divisible by
4 but not by 100.
Your program should clearly describe the functionality of each
function and should display the instructions on how to run the
program.
Your need to create one method “displayMonth” for print each month as required. You can choose return method or not that depend on your design.
Sample Input:
Enter the year for which you wish to generate the calendar:
2004
Enter the day of the week that January first is on: 4
Sample output:
Calendar for year 2004
January
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 31
February
Sun Mon
Tue Wed
Thu
Fri Sat
1
2
3
4
5
6 7
..
..
..
..
..
.. ..
.. ..
The c# program is as follows:
**************************************************************************************************************************************************
using System;
public class Program
{
public static void Main()
{
Console.Write("Enter the year for
which you wish to generate the calendar:");
int year
=Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the day of the
week that January first is on:");
int current =
Convert.ToInt32(Console.ReadLine());
printCalendar(year,current);
}
static string getMonthName(int monthNumber)
{
string[] months = {"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (months[monthNumber]);
}
static int numberOfDays (int monthNumber, int year)
{
// January
if (monthNumber == 0)
return 31;
// February
if (monthNumber == 1)
{
// If the year is leap then February has
// 29 days
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0))
return 29;
else
return 28;
}
// March
if (monthNumber == 2)
return 31;
// April
if (monthNumber == 3)
return 30;
// May
if (monthNumber == 4)
return 31;
// June
if (monthNumber == 5)
return 30;
// July
if (monthNumber == 6)
return 31;
// August
if (monthNumber == 7)
return 31;
// September
if (monthNumber == 8)
return 30;
// October
if (monthNumber == 9)
return 31;
// November
if (monthNumber == 10)
return 30;
// December
if (monthNumber == 11)
return 30;
return 30;
}
static void printCalendar(int year,int current)
{
Console.WriteLine(" Calendar for year - " + year);
int days;
// i --> Iterate through all the months
// j --> Iterate through all the days of the
// month - i
for (int i = 0; i < 12; i++)
{
days = numberOfDays (i, year);
// Print the current month name
Console.WriteLine( getMonthName (i));
// Print the columns
Console.WriteLine(" Sun Mon Tue Wed Thu Fri Sat\n");
// Print appropriate spaces
int k;
for (k = 0; k < current; k++)
Console.Write(" ");
for (int j = 1; j <= days; j++)
{
Console.Write(string.Format("{0,5}", j));
if (++k > 6)
{
k = 0;
Console.Write("\n");
}
}
if (k!=0)
Console.Write("\n");
current = k;
}
return;
}
}
*********************************************************************************************************************************************
output: