Print calendar for a given year in C++


In this problem, we are given a year and we want to print the calendar for that year.

The year calendar shows all days, months on every date of the month. And here we will create a program that will return the calendar of the current year.

For this, we will need some calculations like,

Number of days in a specific month

January, March, May, July, August, October, December has 31 days.

February has 28 days in a nonleap year and 29 days in a leap year.

April, June, September, November has 30 days.

Start Day (weekday) on the month

Based on the year and month, the weekday is found for the 1st of every month.

Example

Now, let’s create a program to print the calendar of 2020 −

 Live Demo

#include<iostream>
using namespace std;
int dayNumber(int day, int month, int year){
   static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
   year -= month < 3;
   return ( year + year/4 - year/100 + year/400 + t[month-1] + day) % 7;
}
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){
   switch(monthNumber){
      case 0 :
      case 2 :
      case 4 :
      case 6 :
      case 7 :
      case 9 :
      case 11: return(31);
      break;
      case 1 :
         if (year % 400 == 0 || (year % 4 == 0 && year %100 != 0))
            return (29);
         else
            return (28);
      break;
      case 3 :
      case 5 :
      case 8 :
      case 10 : return(30);
      break;
   }
}
void printCalendar(int year){
   cout<<"\t\t\t Calendar - Year "<<year;
   int days;
   int current = dayNumber (1, 1, year);
   for (int i = 0; i < 12; i++){
      days = numberOfDays (i, year);
      cout<<endl<<"\t\t ----X----"<<getMonthName (i).c_str()<<"----X---- \t\t"<<endl;
      cout<<" Sun Mon Tue Wed Thu Fri Sat \n";
      int k;
      for (k = 0; k < current; k++)
         cout<<"\t";
      for (int j = 1; j <= days; j++){
         printf("%5d", j);
         if (++k > 6){
            k = 0;
            cout<<endl;
         }
      }
      if (k)
         cout<<endl;
         current = k;
      }
   return;
}
int main(){
   int year = 2019;
   printCalendar(year);
   return (0);
}

Output

Calendar - Year 2019
----X----January----X----
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
----X----February----X----
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
----X----March----X----
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
----X----April----X----
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
----X----May----X----
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
----X----June----X----
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
----X----July----X----
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
----X----August----X----
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
----X----September----X----
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
----X----October----X----
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
----X----November----X----
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
----X----December----X----
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

Updated on: 03-Jan-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements