C Programming (Turbo C++ Compiler) Displaying Yearly Calendar Given Year and First Day of Year
I have given here a C Program for Displaying Yearly Calendar Given Year and the first day of Year. It will validate the leap year and it will display the day (Monday, Tuesday, ...) correctly for the whole year.
Source Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int IsLeapYear(int year)
{
if((year % 4) == 0)
{
if((year % 100) == 0)
{
if( (year % 400) == 0)
return 1;
else
return 0;
}
else
return 1;
}
return 0;
}
int main()
{
static int arrnumdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static int arrleapnumdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static char *strMonthName[] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec",
};
int numdays;
int *pdaysinmonth = 0;
int year, firstdayofyear, firstdayofmonth, month, i, j;
::system("cls");
printf("Enter First Day of Year(1 - Sunday, 7 - Saturday): ");
scanf("%d", &firstdayofyear);
printf("Enter Year: ");
scanf("%d", &year);
if(firstdayofyear < 1 || firstdayofyear > 7)
{
printf("Invalid First Day Of Year. Enter 1 - Sunday, ..., 7 - Saturday.\n");
return 0;
}
pdaysinmonth = ( IsLeapYear(year)) ? arrleapnumdays : arrnumdays;
::system("cls");
printf("\n");
firstdayofmonth = 0;
for(month = 0; month < 12; month++)
{
printf("\n\n\n\t%s %d\n\n", strMonthName[month], year);
printf(" S M T W T F S\n");
numdays = pdaysinmonth[month];
if( month == 0)
firstdayofmonth = firstdayofyear;
else
{
firstdayofmonth = (firstdayofmonth + pdaysinmonth[month-1]) % 7;
if(firstdayofmonth == 0)
firstdayofmonth = 7; // 7 for saturday
}
for(j = 0; j < firstdayofmonth - 1; j++)
printf(" ");
for(i = 0; i < numdays; i++)
{
char buf[32];
sprintf(buf, " %d ", i + 1);
if(i < 9)
strcat(buf, " ");
printf(buf);
if( (i + firstdayofmonth) % 7 == 0)
printf("\n");
}
}
printf("\n\n");
return 0;
}
Click here to get the Turbo C Source code and executable
Output
Enter First Day of Year(1 - Sunday, 7 - Saturday): 3
Enter Year: 2008
Jan 2008
S M T W T F S
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
Feb 2008
S M T W T F S
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
Mar 2008
S M T W T F S
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
Apr 2008
S M T W T F S
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
May 2008
S M T W T F S
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
Jun 2008
S M T W T F S
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
Jul 2008
S M T W T F S
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
Aug 2008
S M T W T F S
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
Sep 2008
S M T W T F S
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
Oct 2008
S M T W T F S
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
Nov 2008
S M T W T F S
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
Dec 2008
S M T W T F S
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
Enter First Day of Year(1 - Sunday, 7 - Saturday): 6
Enter Year: 2010
Jan 2010
S M T W T F S
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
Feb 2010
S M T W T F S
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
Mar 2010
S M T W T F S
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
Apr 2010
S M T W T F S
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
May 2010
S M T W T F S
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
Jun 2010
S M T W T F S
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
Jul 2010
S M T W T F S
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
Aug 2010
S M T W T F S
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
Sep 2010
S M T W T F S
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
Oct 2010
S M T W T F S
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
Nov 2010
S M T W T F S
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
Dec 2010
S M T W T F S
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
|