In this article, you will learn how to write a C program to display a month-by-month calendar for a given year. I have already written a code to display a calendar for the given month and year. This code compiles in Code Blocks with the GCC compiler.
The following is a C program to display a month-by-month calendar for a given year.
#include<stdio.h> #include<string.h> #include<stdlib.h> //check leap year #define IS_LEAP_YEAR(Year) \ ((Year%4==0 && Year%100 != 0)||Year%400==0) /*Function returns the index of the days of the given month and year using ZELLER'S ALGORITHM For e.g- Index Day 0 Sunday 1 Monday 2 Tuesday 3 Wednesday 4 Thursday 5 Friday 6 Saturday*/ int getZeller(int Month,int Year) { int Day = 1, ZMonth, ZYear, Zeller; if(Month < 3) ZMonth = Month+10; else ZMonth = Month-2; if(ZMonth > 10) ZYear = Year-1; else ZYear = Year; Zeller = ((int)((13*ZMonth-1)/5)+Day+ZYear%100+ (int)((ZYear%100)/4)-2*(int)(ZYear/100)+ (int)(ZYear/400)+77)%7; return Zeller; } /* A Function to return the number of days in a month Month Number Name Number of Days 0 January 31 1 February 28 (non-leap) / 29 (leap) 2 March 31 3 April 30 4 May 31 5 June 30 6 July 31 7 August 31 8 September 30 9 October 31 10 November 30 11 December 31 */ int getNumberOfDays(int monthNumber, int year) { int MonthDay[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; MonthDay[monthNumber-1] += (IS_LEAP_YEAR(year) && (monthNumber == 2)); return (MonthDay[monthNumber-1]); } //Align the message void printMessageCenter(const char* message) { int len =0; int pos = 0; //calculate how many space need to print len = (78 - strlen(message))/2; printf("\t\t\t"); for(pos =0 ; pos < len ; pos++) { //print space printf(" "); } //print message printf("%s",message); } // Function to print the calendar of the given year void printCalendar(int month,int year) { //get month name char * months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int j,k; int days; // Index of the day from 0 to 6 int current = getZeller(month,year); days = getNumberOfDays (month, year); printf("\n\n\t\t%s\n", months[month-1]); // Print days in the columns printf(" Sun Mon Tue Wed Thu Fri Sat\n"); // Print appropriate spaces for (k = 0; k < current; k++) printf(" "); //print days as per the month for (j= 1; j <= days; j++) { printf("%5d", j); ++k; //change new line for each week if (k > 6) { k = 0; printf("\n"); } } if (k) printf("\n"); current = k; } void headMessage(const char *message) { system("cls"); printf("\t\t\t#################################################################"); printf("\n\t\t\t############ ############"); printf("\n\t\t\t############ C Project: Calendar For Given Month. ############"); printf("\n\t\t\t############ #######"); printf("\n\t\t\t#################################################################"); printf("\n\t\t\t------------------------------------------------------------------\n"); printMessageCenter(message); printf("\n\t\t\t-------------------------------------------------------------------"); } void welcomeMessage() { headMessage("www.aticleworld.com"); printf("\n\n\n\n\n"); printf("\n\t\t\t **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n"); printf("\n\t\t\t =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); printf("\n\t\t\t = Program ="); printf("\n\t\t\t = Shows ="); printf("\n\t\t\t = calendar of a ="); printf("\n\t\t\t = Given Month ="); printf("\n\t\t\t = ="); printf("\n\t\t\t =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); printf("\n\t\t\t **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n"); printf("\n\n\n\t\t\t Enter any key to continue....."); getchar(); system("cls"); printf("\n\tThis program shows calendar of a given month.\n\n"); } void printCalendarGivenYear(int year) { int month; const int monthInYear = 12; char buf[32] = {0}; sprintf(buf,"%s%d\n\n","Calendar - ",year); printMessageCenter(buf); for(month = 1; month < monthInYear; ++month) { printCalendar(month,year); } } int main() { int Month, Year; welcomeMessage(); //validate input while(1) { fflush(stdin); printf("Enter the Year YYYY = "); scanf("%d",&Year); if(Year < 0) { printf("\nInvalid year value..."); continue; } if(Year < 100) Year += 1900; if(Year < 1582 || Year > 4902) { printf("\nInvalid year value...\n"); printf("Enter the Year between 1582-4902\n\n"); continue; } break; } /*Break loop after valid input*/ //clean console system("cls"); do { printCalendarGivenYear(Year); //Display message printf("\n\n\t\t(*) Press Up Arrow for Next Year."); printf("\n\n\t\t(*) Press Down Arrow for Previous Year."); printf("\n\n\t\t(*) Press ESC for Exit.\n\n\n\t\t"); char KeyCode = getchar(); /* getting Key Code */ if(KeyCode == 72) /* Up Arrow */ Year++; if(KeyCode == 80) /* Down Arrow */ Year--; if(KeyCode == 27) /* Esc button */ { system("cls"); printf("\n\n\n\n\n\t\tBye Bye ..."); printf("\n\t\tThanks for reading Aticleworld\n\n\n\n\n\n"); exit(1); } system("cls"); } while(1); return (0); }
Output:
Calendar - 2023 January 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 February 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 March 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 April 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 May 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 June 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 July 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 August 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 September 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 October 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 November 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 (*) Press Up Arrow for Next Year. (*) Press Down Arrow for Previous Year. (*) Press ESC for Exit.
Recommended Post:
- Check date validity in C.
- C program to find the number of days in a month.
- Print a calendar for a given month for a year.
- C Programming Courses And Tutorials.
- CPP Programming Courses And Tutorials.
- Python Courses and Tutorials.
- Find the number of days between two given dates.
- Count of Leap Years in a given year range.
- Find the prime number using the C program.
- Using the C program to check valid date (date is valid or not)
- Check expiry date Using C program
- C program to print day name of week
- Convert number of days in terms of Years, Weeks and Days using C program
- C program to find century for a year.