-
Notifications
You must be signed in to change notification settings - Fork 23
/
Code for viewing Calender of any year
135 lines (131 loc) · 2.83 KB
/
Code for viewing Calender of any year
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*This code is for viewing the calender of any year entered by the user.
Contributed by Rishi*/
#include<iostream>
using namespace std;
void month(int x)
{
//Function to display the months of the entered year
if(x==1)
cout<<"January ";
else if(x==2)
cout<<"February ";
else if(x==3)
cout<<"March ";
else if(x==4)
cout<<"April ";
else if(x==5)
cout<<"May ";
else if(x==6)
cout<<"June ";
else if(x==7)
cout<<"July ";
else if(x==8)
cout<<"August ";
else if(x==9)
cout<<"September ";
else if(x==10)
cout<<"October ";
else if(x==11)
cout<<"November ";
else if(x==12)
cout<<"December ";
}
int checkLeap(int y)
{
//Function to check leap year
return (y%4==0)?((y%100==0)?((y%400==0)?(1):0):1):0;
}
int getFirstDay(int y)
{
//Function to retrieve the first day of the year
int x=6;
for(int i=1600;i<y;i++)
{
if(checkLeap(i))x+=2;
else x+=1;
x=x>7?x-7:x;
}
return x;
}
int printCal(int x,int m,int leap,int y)
{
//Will to return the monthS to function
cout<<"\t\t\t\t\t ";
month(m);
cout<<"%d\t\t\t\t\t"<<y;
printf("\n");
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
m=31;
}
else
{
m=(m==2&&leap)?29:(m==2?28:30);
}
// Will print the days of the week
cout<<"\tMON\tTUE\tWED\tTHU\tFRI\tSAT\tSUN"<<endl;
int a=1;
for(int i=0;i<=6;i++)
{
for(int j=1;j<=7;j++)
{
if(i!=0||i==0&&j>=x)
{
cout<<"\t"<<a;
a++;
}
else
{
cout<<"\t";
}
if(a==(m+1))
{
a=j;
goto stop;
}
}
cout<<endl;
}
stop:cout<<"";
cout<<"\n\n\t\t***\t\n\n";
return a+1;
}
void printCalendar(int y)
{
//Function to print calender
int a=getFirstDay(y);
for(int i=1;i<=12;i++)
{
a=printCal(a,i,checkLeap(y),y);
}
}
void calling()
{
int t=1;
do
{
int y;
cout<<"Enter a year"<<endl;
cin>>y;
int c1=1;
if(y>=1600&&y<=3000)
{
printCalendar(y);
c1++;
}
else
{
cout<<"Enter a valid year"<<endl;
}
cout<<"Enter 1 to see calendar of another year"<<endl;
cin>>t;
}while(t==1);
if(t!=1)
{
cout<<"Hey thank you for seeing the calender"<<endl;
}
}
int main()
{
calling();
}