Skip to content

Commit 22fd1f2

Browse files
authored
Add files via upload
1 parent d986bd2 commit 22fd1f2

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

calander2.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
#include<iostream>
3+
4+
using namespace std;
5+
6+
// Function to get the first day of the year
7+
int GetFirstDayOfTheYear(int year)
8+
{
9+
int day = (year * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;
10+
return day;
11+
}
12+
13+
int main ()
14+
{
15+
// Arrays to hold the names of the months and the number of days in each month
16+
char *month[] = {"January", "February", "March", "April" , "May", "June", "July", "August", "September", "October", "November", "December"};
17+
int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
18+
19+
int i, j, totalDays, weekday = 0, spaceCounter = 0, year;
20+
21+
// Asking user for the year input
22+
cout << "Enter a year: ";
23+
cin >> year;
24+
25+
// Print the year
26+
cout << "Welcome to the year " << year << endl;
27+
28+
// Check for leap year
29+
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
30+
{
31+
daysInMonth[1] = 29; // February has 29 days in a leap year
32+
}
33+
34+
// Get the first day of the year
35+
weekday = GetFirstDayOfTheYear(year);
36+
37+
// Loop through each month
38+
for (i = 0; i < 12; i++)
39+
{
40+
// Print month name
41+
cout << endl << "________" << month[i] << "________" << endl;
42+
cout << "Sat Sun Mon Tue Wed Thu Fri" << endl;
43+
44+
// Print leading spaces for the first row of dates
45+
for (spaceCounter = 1; spaceCounter <= weekday; spaceCounter++)
46+
{
47+
cout << " ";
48+
}
49+
50+
// Get the number of days in the current month
51+
totalDays = daysInMonth[i];
52+
53+
// Print the days of the month
54+
for (j = 1; j <= totalDays; j++)
55+
{
56+
cout << j << " ";
57+
weekday++;
58+
if (weekday > 6) // Move to the next week
59+
{
60+
weekday = 0;
61+
cout << endl;
62+
}
63+
}
64+
cout << endl;
65+
}
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)