This post contains a total of 7+ C Calendar Program Examples with Source Code. All these Calendar Programs are made using C Language.
You can use the source code of these examples with credits to the original owner.
Related Posts
C Calendar Programs
1. Annual Calendar
Made by Ace. Source
Year? (Ex: 2018): 2022 Jan 2022 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 Feb 2022 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 .....
/**
* Displays a calendar for a given year
* @Input: year: Year to generate calendar
* for
* @OnError: If no year is entered, either
* the current year is used or the
* year this code was written if
* there's a calculation error
*/
#include <stdio.h>
#include <time.h>
#define DEFAULT 2018
/*
* Remember the rhyme
* 30 days past September,
* April, June, and November,
* February has 28 alone
* All the rest have 31
* Except in Leap Year, that's the time
* When February's Days are 29
* NOTE: –Added 'dummy' value at index 0 in
* order to directly index by months
* –Leap year is addressed later in the
* code
*/
int monthMaxDays[]= {0,31,28,31,
30,31,30,31,
31,30,31,30,
31};
char *months[]= {" ", "Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sept", "Oct", "Nov",
"Dec" };
/*
* Utility function returning a valid year if
* the user does not provide one. Uses the
* system time to return the current year
* if possible
* @return The current year, or the year
* this code was written if not possible
*/
unsigned int currentYear(void)
{
char yr[5];
time_t curr;
struct tm* time_info;
time(&curr);
time_info = localtime(&curr);
int year = 0;
strftime(yr, 5, "%Y", time_info);
return sscanf(yr, "%d", &year)==1 ? year : DEFAULT;
}
/*
* Reads in a year from the user if possible
* @return User int representing calendar
* year or one from currentYear() if * not valid
*/
unsigned int readYear(void)
{
int year;
printf("Year? (Ex: 2018): ");
return (scanf("%d", &year) == 1) && year >-1 ? year : currentYear();
}
/*
* Determine the day of the week of the
* calendar year
* @param year: int representing the year to
* check
* @return The starting day of the week for
* the calendar year
*/
int getStart(int year)
{
int startDay;
int delta1, delta2, delta3;
delta1 = (year - 1.)/ 4.0;
delta2 = (year - 1.)/ 100.;
delta3 = (year - 1.)/ 400.;
startDay = (year + delta1 - delta2 + delta3) % 7;
return startDay;
}
/*
* Utility function to determine if year is
* a leap year and adjust Feb's days if so
* @param year: int representing the year to
* check
*/
void leapYear(int year)
{
monthMaxDays[2] = ((!(year % 4) && (year % 100)) || !(year % 400)) ? 29 : 28;
}
/*
* Print out the calendar
* @param year: int representing the calendar
* year
* @param startDay: int representing the
* calendar start day of 1/1
*/
void printCalendar(int year, int startDay) {
int month, day;
for(month=1; month<=12; month++)
{
printf("\n\n%s %d\n", months[month], year);
printf("Sun Mon Tue Wed Thu Fri Sat\n" );
// Position the first date
for(day=1; day<=((startDay)*4); day++)
{
printf("%c", ' ');
}
// Print all days in month
for(day=1; day<=monthMaxDays[month]; day++)
{
printf("%3d",day );
// Is day before Sat?
if((day + startDay) % 7 > 0)
printf(" ");
//Begin new week
else
printf("\n" );
}
// Position following month
startDay = (startDay + monthMaxDays[month]) % 7;
}
}
/*
* Driver
* Read the year, get the starting day of the
* week, adjust for leap year if necessary,
* then prints the calendar
*/
int main(void)
{
unsigned int year;
int startDay;
year = readYear();
printf("%d", year);
startDay = getStart(year);
leapYear(year);
printCalendar(year, startDay);
printf("\n");
}
/**
* @uthor: Ace
* Bugs: Formatting issues–FIXED.
* Version:
* 04202018.1730 - Separated logic of driver
* into separate functions
* 04202018.1900 - Added error checks to
* readYear(), currentYear()
* 04212018.1402 - Addressed formatting
* issues. Many thanks to
* @Baptiste E. Prunier
* 04212018.1555 - Disallow negative years
*/
2. Day Calendar
Made by AyyGee. Source
2022 09 14 Wednesday
/*
Calendar
Day of week
1. p counts extra days, when year is leap (year%4==0)
2. c counts the number of input day from calendar's begining +7(one week) -2 (the 5 day after calendar's begining is Monday)
3. date is the number of input day in week
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Made by Akschine-Maschine 0-0\n");
int year, month, day, date, c, p;
//printf("Input year: ");
scanf("%d", &year);
//printf("Input month: ");
scanf("%d", &month);
//printf("Input day: ");
scanf("%d", &day);
if (day<1||day>31)
{
printf("Incorrect input!");
return 0;
}
p=year/4;
switch (month)
{
case 1:
c=day+year*365+p-2;
break;
case 2:
c=31+day+year*365+p-2;
break;
case 3:
c=59+day+year*365+p-2;
break;
case 4:
c=90+day+year*365+p-2;
break;
case 5:
c=120+day+year*365+p-2;
break;
case 6:
c=151+day+year*365+p-2;
break;
case 7:
c=181+day+year*365+p-2;
break;
case 8:
c=212+day+year*365+p-2;
break;
case 9:
c=243+day+year*365+p-2;
break;
case 10:
c=273+day+year*365+p-2;
break;
case 11:
c=304+day+year*365+p-2;
break;
case 12:
c=334+day+year*365+p-2;
break;
default:
printf("Incorrect input!");
break;
}
date=c%7;
switch(date)
{
case 0:
printf("Sunday");
break;
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
default:
printf("Saturday");
break;
}
return 0;
}
3. Calendar of a given year
Made by Murali Hemanth. Source
CALENDAR OF YEAR 2012 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 29 .....
//Enter a year
#include <stdio.h>
int main()
{
int y,i,d,j,k,a;
char month[12][10]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
scanf("%d",&y);
i=(y%4==0)&&((y%100!=0)||(y%400));
d=y+y/4-y/100+y/400-i;
d=d%7;
int dm[12]={31,28+i,31,30,31,30,31,31,30,31,30,31};
printf(" CALENDAR OF YEAR %d\n\n",y);
for(j=0;j<12;j++)
{
printf(" %s\n",month[j]);
printf("SUN MON TUE WED THU FRI SAT\n");
for(a=0;a<d;a++)
{
printf(" ");
}
k=1;
for(a=d;a<7;a++,k++)
{
printf("%2d ",k);
}
printf("\n");
for(;k<=dm[j];)
{
for(a=0;a<7&&k<=dm[j];k++,a++)
{
printf("%2d ",k);
}
printf("\n");
}
printf("\n");
d=a%7;
}
return 1;
}
4. Day of Week Calendar
Made by Swetha SM. Source
Enter the year : 2000 Enter the month : 12 Enter the date : 2 The Date Given is : 2 / 12 / 2000 Day of Week of the Date is : Friday
#include<stdio.h>
#include<math.h>
int main(){
int dat, mont, years;
printf("Enter the year : ");
scanf("%d", &years);
printf("\n Enter the month : ");
scanf("%d", &mont);
printf("\n Enter the date : ");
scanf("%d", &dat);
weekday(dat, mont, years);
return 0;
}
int weekday(int date, int month, int year) {
int dayWeek;
printf("\nThe Date Given is : %d / %d / %d \n\n", date, month, year);
dayWeek = Y(year)+M(month,year)+date;
dayWeek = dayWeek % 7;
switch (dayWeek)
{
case 0: printf("Day of Week of the Date is : Saturday");
break;
case 1: printf("Day of Week of the Date is : Sunday");
break;
case 2: printf("Day of Week of the Date is : Monday");
break;
case 3: printf("Day of Week of the Date is : Tuesday");
break;
case 4: printf("Day of Week of the Date is : Wednesday");
break;
case 5: printf("Day of Week of the Date is : Thursday");
break;
case 6: printf("Day of Week of the Date is : Friday");
break;
default: printf("The Given input data is wrong");
}
return 0;
}
int M(int months, int yearss){
int sum=0,i,f;
if ((yearss % 100 == 0) && (yearss % 400 != 0))
f= 0;
else if (yearss % 4 == 0)
f= 1;
else
f= 0;
int mon[13]={3,f,3,2,3,2,3,3,2,3,2,3};
for(i=0;i<(months-1);i++)
sum=sum+mon[i];
return sum;
}
int Y(int yea){
int y,a;
y=yea%4;
a=((y*2)+(yea-y));
return a;
}
5. Current Date & Time with Calendar
Made by Noble Khan. Source
Current Date :Sep 14 2022 UTC Current Time:06:42:58 UTC enter the year= Calendar - 2000 ------------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 29 .....
#include <stdio.h>
#include <string.h>
// C program to print the month by month
// calendar for the given year
#include <stdio.h>
// Function that returns the index of the
// day for date DD/MM/YYYY
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;
}
// Function that returns the name of the
// month for the given month Number
// January - 0, February - 1 and so on
char* getMonthName(int monthNumber)
{
char* month;
switch (monthNumber) {
case 0:
month = "January";
break;
case 1:
month = "February";
break;
case 2:
month = "March";
break;
case 3:
month = "April";
break;
case 4:
month = "May";
break;
case 5:
month = "June";
break;
case 6:
month = "July";
break;
case 7:
month = "August";
break;
case 8:
month = "September";
break;
case 9:
month = "October";
break;
case 10:
month = "November";
break;
case 11:
month = "December";
break;
}
return month;
}
// Function to return the number of days
// in a month
int numberOfDays(int monthNumber, int year)
{
// January
if (monthNumber == 0)
return (31);
// February
if (monthNumber == 1) {
// If the year is leap then Feb
// has 29 days
if (year % 400 == 0
|| (year % 4 == 0
&& year % 100 != 0))
return (29);
else
return (28);
}
// March
if (monthNumber == 2)
return (31);
// April
if (monthNumber == 3)
return (30);
// May
if (monthNumber == 4)
return (31);
// June
if (monthNumber == 5)
return (30);
// July
if (monthNumber == 6)
return (31);
// August
if (monthNumber == 7)
return (31);
// September
if (monthNumber == 8)
return (30);
// October
if (monthNumber == 9)
return (31);
// November
if (monthNumber == 10)
return (30);
// December
if (monthNumber == 11)
return (31);
}
// Function to print the calendar of
// the given year
void printCalendar(int year)
{
printf(" Calendar - %d\n\n", year);
int days;
// Index of the day from 0 to 6
int current = dayNumber(1, 1, year);
// i for Iterate through months
// j for Iterate through days
// of the month - i
for (int i = 0; i < 12; i++) {
days = numberOfDays(i, year);
// Print the current month name
printf("\n ------------%s-------------\n",
getMonthName(i));
// Print the columns
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
// Print appropriate spaces
int k;
for (k = 0; k < current; k++)
printf(" ");
for (int j = 1; j <= days; j++) {
printf("%5d", j);
if (++k > 6) {
k = 0;
printf("\n");
}
}
if (k)
printf("\n");
current = k;
}
return;
}
// Driver Code
int main()
{
char curr_time[10];
char curr_date[12];
int std_c;
printf("Current Date :%s ",__DATE__);
printf("UTC\n");
printf("Current Time:%s ",__TIME__);
printf("UTC\n");
int year;
printf("enter the year= \n");
scanf("%d",&year);
// Function Call
printCalendar(year);
return 0;
}
6. C Calendar
Made by Penumudi Lavanya. Source
Year? (Ex:2019):2000 Jan 2000 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 Feb 2000 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 .....
#include<stdio.h>
#include<time.h>
#define DEFAULT 2019
int monthMaxDays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]= {" ","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"};
unsigned int currentYear(void)
{
char yr[5];
time_t curr;
struct tm* time_info;
time(&curr);
time_info = localtime(&curr);
int year = 0;
strftime(yr, 5,"%Y", time_info);
return sscanf(yr, "%d", &year)==1 ?year:DEFAULT;
}
unsigned int readYear(void)
{
int year;
printf("Year?\n(Ex:2019):");
return(scanf("%d",&year)==1)&& year>-1? year:currentYear();
}
int getstart(int year)
{
int startday;
int delta1,delta2,delta3;
delta1=(year-1.)/4.0;
delta2=(year-1.)/100;
delta3=(year-1.)/400;
startday=(year+delta1-delta2+delta3)%7;
return startday;
}
void leapyear(int year)
{
monthMaxDays[2]=((!(year%4)&&(year%100))||!(year%400))?29:28;
}
void printCalendar(int year,int startday)
{
int month,day;
for(month=1;month<=12;month++)
{
printf("\n\n%s %d\n",months[month],year);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(day=1;day<=monthMaxDays[month];day++)
{
printf("%3d",day);
if((day+startday)%7>0)
printf(" ");
else
printf("\n");
}
startday=(startday+monthMaxDays[month])%7;
}
}
int main(void)
{
unsigned int year;
int startday;
year=readYear();
printf("%d",year);
startday=getstart(year);
leapyear(year);
printCalendar(year,startday);
printf("\n");
}
7. Simple Calendar Program
Made by Sujan. Source
What year is it?: 2012 Sunday =0 Monday =1 Tuesday =2 Wednsday=3 Thursday=4 Friday =5 Saturday=6 What is the first day of January?:January Su Mo Tu We Th Fr Sa 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 Su Mo Tu We Th Fr Sa 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
#include <stdio.h>
int isleapyear(int x);
void printWeekHeader();
void printMonthHeader();
void printdays(int m);
int daystart;
int year;
int main()
{
printf("What year is it?:");
scanf("%d",&year);
printf("\n\nSunday =0\n");
printf("Monday =1\n");
printf("Tuesday =2\n");
printf("Wednsday=3\n");
printf("Thursday=4\n");
printf("Friday =5\n");
printf("Saturday=6");
printf("\n\nWhat is the first day of January?:");
scanf("%d",&daystart);
getchar();
printMonthHeader();
getchar();
}
void printWeekHeader()
{
printf("Su Mo Tu We Th Fr Sa\n");
}
int isleapyear (int x)
{
if (year==0)
{
return 0;
}
else
{
if (year%100!=0 && year%4==0)
{
return 1;
}
else if (year%400==0)
{
return 1;
}
else
{
return 0;
}
}
}
void printdays(int m)
{
int count=0, looplimit,i;
int x;
int s[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if (m==1)
{
x=isleapyear(year);
looplimit=s[m]+x;
}
else
{
looplimit=s[m];
}
for (i=0;i<daystart;i++)
{
printf(" ");
count++;
}
for (i=1;i<=looplimit;i++)
{
printf("%2d ",i);
count ++;
if ( count==7)
{
printf("\n");
count=0;
}
}
daystart=count;
printf("\n");
}
void printMonthHeader()
{
printf("January\n");
printWeekHeader();
printdays(0);
printf("\n");
getchar();
printf("February\n");
printWeekHeader();
printdays(1);
printf("\n");
getchar();
printf("March\n");
printWeekHeader();
printdays(2);
printf("\n");
getchar();
printf("April\n");
printWeekHeader();
printdays(3);
printf("\n");
getchar();
printf("May\n");
printWeekHeader();
printdays(4);
printf("\n");
getchar();
printf("June\n");
printWeekHeader();
printdays(5);
printf("\n");
getchar();
printf("July\n");
printWeekHeader();
printdays(6);
printf("\n");
getchar();
printf("August\n");
printWeekHeader();
printdays(7);
printf("\n");
getchar();
printf("September\n");
printWeekHeader();
printdays(8);
printf("\n");
getchar();
printf("October\n");
printWeekHeader();
printdays(9);
printf("\n");
getchar();
printf("November\n");
printWeekHeader();
printdays(10);
printf("\n");
getchar();
printf("December\n");
printWeekHeader();
printdays(11);
printf("\n");
}
8. Calendar by Ayoub Maghdaoui
Made by Ayoub Maghdaoui. Source
Please enter a year (example: 1999) : 2001 ------------------------- 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 ------------------------- ....
// Calendar : Enter the year to get its calendar
#include<stdio.h>
#define TRUE 1
#define FALSE 0
//each month it contains days bellow starting from January
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
//this function has just to get the year of the calendar
int inputyear(void)
{
int year;
printf("Please enter a year (example: 1999) : ");
scanf("%d", &year);
return year;
}
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year - 1.)/ 4.0;
d2 = (year - 1.)/ 100.;
d3 = (year - 1.)/ 400.;
daycode = (year + d1 - d2 + d3) %7;
return daycode;
}
int determineleapyear(int year)
{
if((year% 4 == FALSE && year%100 != FALSE) || year%400 == FALSE)
{
days_in_month[2] = 29;
return TRUE;
}
else
{
days_in_month[2] = 28;
return FALSE;
}
}
void calendar(int year, int daycode)
{
int month, day;
for ( month = 1; month <= 12; month++ )
{ printf("\n-------------------------");
printf("\n\t\t %s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n" );
// Correct the position for the first date
for ( day = 1; day <= 1 + daycode * 5; day++ )
{
printf(" ");
}
// Print all the dates for one month
for ( day = 1; day <= days_in_month[month]; day++ )
{
printf("%2d", day );
// Is day before Sat? Else start next line Sun.
if ( ( day + daycode ) % 7 > 0 )
printf(" " );
else
printf("\n " );
}
// Set position for next month
daycode = ( daycode + days_in_month[month] ) % 7;
}
}
int main(void)
{
int year, daycode;
year = inputyear();
daycode = determinedaycode(year);
determineleapyear(year);
calendar(year, daycode);
printf("\n");
}