Hello coders and viewers :)

this is sudhakar jha from india. i am a software developer with an firm in india. just peeped in this introducing forum to say hi.

:mrgreen:

here is a small code that i have written for retriving the calander of given month and year hope u will enjoy it. i am working on it to improve by adding graphics and public holiday facility too..

/* Developed by Sudhakar Jha... (sudhakar.jha@gmail.com) 
   This program works and tested for years between 1800 & 2099
   it will work for year greater also. but not tested
   so use it on ur own risk.
   
   any comments and bugs are most welcome
   pls feel free to send them to my mail box
   
   cheers!!!
*/
   

#include<stdio.h>
#include<ctype.h>

/*Function to test if a given year is leap year or not*/
#define isleapyear(year) (!((year%4) && (year%100)) || !((year%400) && (year%1000)))
/*int months[13]={0,1,2,3,4,5,6,7,8,9,10,11,12}; To store the months in digit form*/

char monthname[13][15]={"illegal month","January","February","March","April","May","June"
						  ,"July","August","September","October","November"
						  ,"December"}; /*To store the months name in the string format*/

int monthday[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
					{0,31,29,31,30,31,30,31,31,30,31,30,31}}; /*To store the days on the months according to the year*/

char week[7][10] = {                  /*To store the day of the week*/
		  "Monday","Tuesday","Wednesday","Thursday",
		  "Friday","Saturday","Sunday"
		  };
char days[7]={'S','M','T','W','T','F','S'}; /* to display in calander*/
int isvalidinput(int mon,int year);
int weekday(int day,int mon,int year);
void showcal(int mon,int year);
int main(void)
{
 int lYear,lMonth,temp,ans=1; /*Variables to hold the user input*/
 printf("\n*********************** Welcome To Calander Program  ************************\n");
 do {
 printf("Please enter date in mm-yyyy format: ");
 scanf("%d-%d",&lMonth,&lYear);
 /*Check if user has provided the correct input*/
 if (isvalidinput(lMonth,lYear))
 {
  /*
  printf("\n the first day of the month is : %s",week[weekday(1,lMonth,lYear)]);
  printf("\n the month is : %s", monthname[lMonth]);
  for(temp=1;temp<13;temp++)
  {
	printf("\n%s --> %d",monthname[temp],monthday[isleapyear(lYear)][temp]);
  }
  */
  printf("\n\n\t\t\t %s %d\n\n",monthname[lMonth],lYear);
  printf("\n");

  showcal(lMonth,lYear);
 }
 else
 {
  printf("\nThe month and year you provided is not valid");
 }
 printf("\n want to continue 0 -> no / 1 -> yes: ");
 scanf("%d",&ans);
 }while(ans==1);
 return 1;
}

int isvalidinput(int mon,int year)
{
 if ((mon < 13 && mon > 0) && (year > 1800  && year < 2099))
 {
  return 1;
 }
 else
 {
  return 0;
 }
}

int weekday(int day, int month, int year)
{
		  int ix, tx, vx;

		  switch (month) {
					 case 2  :
					 case 6  : vx = 0; break;
					 case 8  : vx = 4; break;
					 case 10 : vx = 8; break;
					 case 9  :
					 case 12 : vx = 12; break;
					 case 3  :
					 case 11 : vx = 16; break;
					 case 1  :
					 case 5  : vx = 20; break;
					 case 4  :
					 case 7  : vx = 24; break;
		  }
		  if (year > 1900)  // 1900 was not a leap year
		  year -= 1900;
		  ix = ((year - 21) % 28) + vx + (month > 2);  // take care of February
		  tx = (ix + (ix / 4)) % 7 + day;              // take care of leap year
		  return (tx % 7);
}

void showcal(int mon,int year)
{
	/*disply the short days with format*/
	int tab,i,day,j=0;
	for(tab=0;tab<7;tab++)
	{
		printf("\t%c",days[tab]);
	}
	tab=weekday(1,mon,year);
	printf("\n");
	/*how many tabs we need for printing first day*/
	if(tab!=6)
	{
	for(i=0;i<tab+2;i++)
	{
		printf("\t");
	}
	}
	else
	{
		printf("\t");
	}
	 /*Get the no of days in the provided month"*/
	 day=monthday[isleapyear(year)][mon];
	 /*start printing the dates*/
	 for(i=1;i<=day;i++)
	 {
		/*check if i & tab sum is exeeding 7*/
		if(j+tab+2>7)
		{
			printf("\n");
			printf("\t%d\t",i);
			j=0;
			tab=0;
		}
		else
		{
			printf("%d\t",i);
			j++;
		}
	 }

}

Recommended Answers

All 3 Replies

Hello,
Welcome to Daniweb :D
Hope you like the C/C++ forums here.

yes.. i liked the forum thats why here to share and learn. Cheers!!

yes.. i liked the forum thats why here to share and learn. Cheers!!

Hello Sudhakar,
Welcome to daniweb... :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.