In: Computer Science
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 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 466 days. That extra day comes at the end of February. Thus, a leap year has 466 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.
Sample Input:
Enter the year for which you wish to generate the calendar:
2018
Enter the day of the week that January first is on: 1
Sample output:
Calendar for year 2018
January
Sun Mon
Tue Wed
Thu
Fri Sat
1
2
4
4
5
6
7 8 9 10 11 12 14
14 15 16 17 18 19 20
21 22 24 24 25 26 27
28 29 40 41
February
Sun Mon
Tue Wed
Thu
Fri Sat
1
2
4
4 5 6 7 8 9 10
11 12 14 .. .. .. ..
.. .. .. .. .. .. ..
..
..
..
code:
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
string getMonthName(int monthNumber)
{
string months[] = {"January", "February",
"March",
"April", "May", "June",
"July", "August",
"September",
"October", "November",
"December"
};
return (months[monthNumber]);
}
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 (31);
}
// Function to print the calendar
void printCalendar(int year,int d)
{
printf (" Calendar -
%d\n\n", year);
int days;
// Index of the day from 0 to 6
int current = d;
for (int i = 0; i < 12; i++)
{
days = numberOfDays (i,
year);
// Print the current
month name
printf("\n
------------%s-------------\n",
getMonthName
(i).c_str());
// Print the
columns
printf("
Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
// printing spaces for
days
int k;
for (k = 0; k < current;
k++)
printf("\t");
for (int j = 1; j <=
days; j++)
{
printf("%5d\t",
j);
if
(++k >6)
{
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
// Driver Program to print calendar
int main()
{
int year,b;
cout<<"enter year:";
cin>>year;
cout<<"enter day of january first in
numbers:";
cin>>b;
cout<<"\n";
printCalendar(year,b);
return (0);
}
/*
Index Day
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday*/
/*
Month Number Name
0 January
1 February
2 March
3 April
4 May
5 June
6 July
7 August
8 September
9 October
10 November
11 December
*/
output:
code screenshot: