Week days and Calender program in C
Introduction
Though all of the programming languages enhanced to provide basic date, time and calendar calculations, writing hard logic for a simple requirements such as calander improves our programming skill. Let us consider about calculating day of a week for a given date and calender for a given month in C language. This is for helping learners of C language.
A little logic
Initially these programs store number of days in all 12 months in an array. Julian Day is the number which represents the day number of that date in all 365 days of that year. To find the julian day of the given date, find the number of days in all the month before to the current month plus the day of the date. For example, for the date 12th of march is number of days in January + february+12. To adjust the number of days difference in february of an ordinary year and the leap year (28 and 29 respectively), we have to check leap year and change the february month days in the array.
Formula
This program employs the following formula,
day of weak = julian day + year + (year – 1)/400) + ((year – 1)/4) – (year – 1)/100)) mod 7
In the Day of Week finder we add the day in the given date. but in the calendar program, the day of the date is not specified exclusively, instead by default day 1 will be considered as day of the date. This is because we need to print the calendar for the given month and year. If the day of week is zero the date 1 of that month and year is, saturday, if 1, it is sunday and so on.
#include< stdio.h>
#include< conio.h>
void main()
{
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char days[7][10]={"Saturday",
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday"};
int m,d,y,fh,h,f,i,jd=0,dow;
clrscr();
printf("Enter the Date (dd mm yyyy): ");
scanf("%d%d%d",&d,&m,&y);
/* To adjust 29 days for the Leap Year */
if (y%4==0) month[2]=29;
for(i=1;i< m; i++)
jd=jd+month[i];
jd=jd+d;
dow=(jd+y+((y-1)/400)+((y-1)/4)-((y-1)/100))%7;
printf("%s",days[dow]);
getch();
}
Formatting the output of calendar program
In this program, after printing the month labels starting from Saturday(each label sized three characters, such as Sat, Sun, etc.,) we have to number of spaces which is equal to “day of week” that we calculated. starting for 1 to number of days in the current month the numbers will be printed with a new line character for each 7 days in order to start with next week saturday.
#include< stdio.h>
#include< conio.h>
void main()
{
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int m,d,y,fh,h,f,i,jd=0,dow,j;
clrscr();
printf("Enter Month : ");
scanf("%d",&m);
printf("Enter the Year : ");
scanf("%d",&y);
/* To adjust 29 days for the Leap Year */
if (y%4==0) month[2]=29;
for(i=1;i< m; i++)
jd=jd+month[i];
jd=jd+1;
dow=(jd+y+((y-1)/400)+((y-1)/4)-((y-1)/100))%7;
printf(" Sat Sun Mon Tue Wed Thu Fri\n");
for(j=0;j< dow;j++) printf("%4s");
for(i=1;i< month[m]; i++)
{
printf("%4d",i);
j=(j+1)%7;
if(j==0) printf("\n");
}
getch();
}









Author: Ganesh Kumar (15 Articles)
Ganesh Kumar has qualified with his Masters in Technology with Distinction. His total experience is about 6 years in Development and 2 years in Teaching. Presently he is working for WDC Ltd., Kolkata, India in C#, .Net and SQL Server.
Leave your response!