Question

In: Computer Science

Write a C# program that prints a calendar for a given year. Call this program calendar....

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
..         ..          ..          ..          ..          ..          ..
..          ..

Solutions

Expert Solution

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:


Related Solutions

Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and "#include<cmath>" 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...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a java program MyCalendar that outputs a monthly calendar for a given month and year....
Write a java program MyCalendar that outputs a monthly calendar for a given month and year. the output should be looks like this one at the bottom:( I don't know why it doesn't show right but the first day of the month is not Sunday it starts from Wednesday) F. Last’s My Calendar Enter month year? 10 2019 10/2019 Sun Mon Tue Wed Thu Fri Sat --------------------------------- 1 2 3 4 5 6 7 8 9 10  11 12 13 14...
Write a C program, char.c, which prints the numerical value of the special character constants given...
Write a C program, char.c, which prints the numerical value of the special character constants given below. Don’t just look up the codes and print them directly from your program. You should have your program output the values of the character constants by using them as string literals within your printf() statements. Your output should be presented in a neat, orderly, tabular format as shown below: Char Constant Description Value '\n' newline '\t' horizontal tab '\v' vertical tab '\b' backspace...
Programming in C++ Write a program that prints the values in an array and the addresses...
Programming in C++ Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
Programming In C Write a program that prints the values in an array and the addresses...
Programming In C Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows: Array index notation using array name Pointer/offset notation using array name Array index notation using a pointer Pointer/offset notation using a pointer Learning Objectives In this assignment, you will: Use functions with array and pointer arguments Use indexing and offset notations to access arrays Requirements Your code must use these eight functions, using these...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT