Calendar

i want to know the 1st day of the 1st yr....

Page 1 of 1

6 Replies - 501 Views - Last Post: 17 July 2009 - 11:52 AM Rate Topic: -----

#1 sree12588  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 09

Calendar

Posted 17 July 2009 - 06:16 AM

Hi guys.. this program can display t calendar of any month or all 12 months of any year and it s also a day detector... my program tells me tht the 1st day of the 1st year is a monday, but this link (http://www.hf.rim.or.jp/~kaji/cal/cal.cgi?1) tells me tht t 1st day of the 1st year is a saturday.. but, my program shows me t correct output for all other inputs.... can anyne help....???

#include<stdio.h>
#include<conio.h>
void monthdisp(void);
void daydetect(void);
void yeardisp(void);
int funcday(int,int);
int funcno(int,int);
void dispmonthname(int,int);
void main()
{
	int a,ch,ch1,i,qw,m,n,yr,mon,day,day1;
	char ch2='y';
	clrscr();
	while(ch2=='y')
	{
		printf("\n\n\t			   *****CALENDER PROGRAM*****\n\n\n");
		printf("1. Month Display\n2. Year Display\n3. Day Detector\n4. Exit\n");
		printf("\nEnter your choice...:");
		scanf("%d",&ch1);
		switch(ch1)
		{
			case 1:
			{
				monthdisp();
				break;
			}
			case 2:
			{
				yeardisp();
				break;
			}
			case 3:
			{
				daydetect();
				break;
			}
			case 4:
			{
				exit(0);
				break;
			}
		}
	}
	getch();
}
void monthdisp()
{
	int a,b=0,g=1,i,c=1,qw,m,n,mon,s=0,yr;
	char mons[10];
	lab1:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct value for year.");
		printf("\nThe value should not be less than 1.\n");
		goto lab1;
	}
	printf("Enter the first three characters of the month: ");
	scanf("%s",&mons);
	mon=calcmonth(mons);
	printf("\n\n");
	n=funcday(yr,mon);
	qw=funcno(yr,mon);
	dispmonthname(mon,yr);
	printf("S\tM\tT\tW\tT\tF\tS\n\n");
	for(c=1;c<=5;c++)
	{
		for(a=1;a<=7;a++)
		{
			if(g==1)
			{
				for(i=1;i<=n;i++)
				{
					s=s+1;
					printf("\t");
						g=0;
				}
			}
			b=b+1;
			if(b>qw)
				break;
			printf("%d\t",B);
			s=s+1;
			if(s%7==0)
				printf("\n\n");
		}
	}
	printf("\n");
	return;
}
void yeardisp()
{
	int yr,i,qw,c,n,k,a;
	lab2:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct input for year.");
		printf("\nThe value should be greater than 1.\n");
		goto lab2;
	}
	for(k=1;k<=12;k++)
	{
		int g=1,s=0,b1=0;
		n=funcday(yr,k);
		qw=funcno(yr,k);
		dispmonthname(k,yr);
		printf("S\tM\tT\tW\tT\tF\tS\n\n");
		for(c=1;c<=5;c++)
		{
			for(a=1;a<=7;a++)
			{
				if(g==1)
				{
					for(i=1;i<=n;i++)
					{
						s=s+1;
						printf("\t");
						g=0;
					}
				}
				b1=b1+1;
				if(b1>qw)
					break;
				printf("%d\t",b1);
				s=s+1;
				if(s%7==0)
					printf("\n\n");
			}
		}
		printf("\n\n");
		getch();
	}
	return;
}
void daydetect()
{
	int yr,mon,day,n,day1;
	char mons[10];
	lab3:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct input for year.\n");
		printf("The value should not be less than 1.\n");
		goto lab3;
	}
	printf("Enter the first three characters of the month: ");
	scanf("%s",&mons);
	mon=calcmonth(mons);
	printf("Enter the day in digits: ");
	scanf("%d",&day);
	if(day<1||day>31)
	{
		printf("Please provide a correct input for day.");
		printf("\nThe value should only be between 1 and 31\n");
		goto lab3;
	}
	n=funcday(yr,mon);
	day1=(day+n)%7;
	switch(day1)
	{
		case 1: printf("\n\t***%dth %s %d is a Sunday***",day,mons,yr);
			break;
		case 2: printf("\n\t***%dth %s %d is a Monday***",day,mons,yr);
			break;
		case 3: printf("\n\t***%dth %s %d is a Tuesday***",day,mons,yr);
			break;
		case 4: printf("\n\t***%dth %s %d is a Wednesday***",day,mons,yr);
			break;
		case 5: printf("\n\t***%dth %s %d is a Thursday***",day,mons,yr);
			break;
		case 6: printf("\n\t***%dth %s %d is a Friday***",day,mons,yr);
			break;
		case 0: printf("\n\t***%dth %s %d is a Saturday***",day,mons,yr);
			break;
	}
	return;
}
int funcday(int yr,int mon)
{
	int a[15]={31,28,31,30,31,30,31,31,30,31,30,31};
	int i,cd,g,w;
	long int ab=0;
	int defyr=1;
	while(yr!=defyr)
	{
		if(defyr%100==0)
			w=400;
		else
			w=4;
		if((defyr%w==0))
		{
			ab=ab+366;
		}
		else
		{
			ab=ab+365;
		}
		defyr=defyr+1;
	}
	if(yr==defyr)
	{
		for(i=0;i<(mon-1);i++)
		{
			ab=ab+a[i];
		}
		if((yr%w==0)&&(mon>2))
			ab=ab+1;
	}
	cd=(ab+1)%7;
	return cd;
}
int funcno(int yr,int mon)
{
	int r;
	switch(mon)
	{
		case 1: r=31;
			break;
		case 2: if(yr%4==0)
				r=29;
			else
				r=28;
			break;
		case 3: r=31;
			break;
		case 4: r=30;
			break;
		case 5: r=31;
			break;
		case 6: r=30;
			break;
		case 7: r=31;
			break;
		case 8: r=31;
			break;
		case 9: r=30;
			break;
		case 10: r=31;
			break;
		case 11: r=30;
			break;
		case 12: r=31;
			break;
	}
	return r;
}
void dispmonthname(int mon,int yr)
{
	if(mon==1)
		printf("\n\t\t *****JANUARY %d*****\n\n",yr);
	else if(mon==2)
		printf("\n\t\t *****FEBRUARY %d*****\n\n",yr);
	else if(mon==3)
		printf("\n\t\t *****MARCH %d*****\n\n",yr);
	else if(mon==4)
		printf("\n\t\t *****APRIL %d*****\n\n",yr);
	else if(mon==5)
		printf("\n\t\t *****MAY %d*****\n\n",yr);
	else if(mon==6)
		printf("\n\t\t *****JUNE %d*****\n\n",yr);
	else if(mon==7)
		printf("\n\t\t *****JULY %d*****\n\n",yr);
	else if(mon==8)
		printf("\n\t\t *****AUGUST %d*****\n\n",yr);
	else if(mon==9)
		printf("\n\t\t *****SEPTEMBER %d*****\n\n",yr);
	else if(mon==10)
		printf("\n\t\t *****OCTOBER %d*****\n\n",yr);
	else if(mon==11)
		printf("\n\t\t *****NOVEMBER %d******\n\n",yr);
	else if(mon==12)
		printf("\n\t\t *****DECEMBER %d*****\n\n",yr);
	return;
}

int calcmonth(char mon1)
{
	int n;
	if(strcmp(mon1,"jan")==0)
		n=1;
	else if(strcmp(mon1,"feb")==0)
		n=2;
	else if(strcmp(mon1,"mar")==0)
		n=3;
	else if(strcmp(mon1,"apr")==0)
		n=4;
	else if(strcmp(mon1,"may")==0)
		n=5;
	else if(strcmp(mon1,"jun")==0)
		n=6;
	else if(strcmp(mon1,"jul")==0)
		n=7;
	else if(strcmp(mon1,"aug")==0)
		n=8;
	else if(strcmp(mon1,"sep")==0)
		n=9;
	else if(strcmp(mon1,"oct")==0)
		n=10;
	else if(strcmp(mon1,"nov")==0)
		n=11;
	else if(strcmp(mon1,"dec")==0)
		n=12;
	else
	{
		printf("Please provide the correct value for month...\n");
		printf("Try again from first\n");
		getch();
		exit(0);
	}
	return n;
}


***added code tags***
-jjsaw5

Is This A Good Question/Topic? 0
  • +

Replies To: Calendar

#2 alm0614  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 85
  • Joined: 23-June 09

Re: Calendar

Posted 17 July 2009 - 06:18 AM

use code tags
Was This Post Helpful? 0
  • +
  • -

#3 sree12588  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-July 09

Re: Calendar

Posted 17 July 2009 - 06:21 AM

hi guys... my program displays the calendar of any month, it can display all 12 months of a yr and can also return the day of any date... according to my program, the 1st day of year 1 is a monday, but according to this link(http://www.hf.rim.or.jp/~kaji/cal/cal.cgi?1) the 1st day of year 1 is a saturday... i am a bit confused and i cannot spot t problem... can any one help...??

PROGRAM:

#include<stdio.h>
#include<conio.h>
void monthdisp(void);
void daydetect(void);
void yeardisp(void);
int funcday(int,int);
int funcno(int,int);
void dispmonthname(int,int);
void main()
{
	int a,ch,ch1,i,qw,m,n,yr,mon,day,day1;
	char ch2='y';
	clrscr();
	while(ch2=='y')
	{
		printf("\n\n\t			   *****CALENDER PROGRAM*****\n\n\n");
		printf("1. Month Display\n2. Year Display\n3. Day Detector\n4. Exit\n");
		printf("\nEnter your choice...:");
		scanf("%d",&ch1);
		switch(ch1)
		{
			case 1:
			{
				monthdisp();
				break;
			}
			case 2:
			{
				yeardisp();
				break;
			}
			case 3:
			{
				daydetect();
				break;
			}
			case 4:
			{
				exit(0);
				break;
			}
		}
	}
	getch();
}
void monthdisp()
{
	int a,b=0,g=1,i,c=1,qw,m,n,mon,s=0,yr;
	char mons[10];
	lab1:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct value for year.");
		printf("\nThe value should not be less than 1.\n");
		goto lab1;
	}
	printf("Enter the first three characters of the month: ");
	scanf("%s",&mons);
	mon=calcmonth(mons);
	printf("\n\n");
	n=funcday(yr,mon);
	qw=funcno(yr,mon);
	dispmonthname(mon,yr);
	printf("S\tM\tT\tW\tT\tF\tS\n\n");
	for(c=1;c<=5;c++)
	{
		for(a=1;a<=7;a++)
		{
			if(g==1)
			{
				for(i=1;i<=n;i++)
				{
					s=s+1;
					printf("\t");
						g=0;
				}
			}
			b=b+1;
			if(b>qw)
				break;
			printf("%d\t",B);
			s=s+1;
			if(s%7==0)
				printf("\n\n");
		}
	}
	printf("\n");
	return;
}
void yeardisp()
{
	int yr,i,qw,c,n,k,a;
	lab2:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct input for year.");
		printf("\nThe value should be greater than 1.\n");
		goto lab2;
	}
	for(k=1;k<=12;k++)
	{
		int g=1,s=0,b1=0;
		n=funcday(yr,k);
		qw=funcno(yr,k);
		dispmonthname(k,yr);
		printf("S\tM\tT\tW\tT\tF\tS\n\n");
		for(c=1;c<=5;c++)
		{
			for(a=1;a<=7;a++)
			{
				if(g==1)
				{
					for(i=1;i<=n;i++)
					{
						s=s+1;
						printf("\t");
						g=0;
					}
				}
				b1=b1+1;
				if(b1>qw)
					break;
				printf("%d\t",b1);
				s=s+1;
				if(s%7==0)
					printf("\n\n");
			}
		}
		printf("\n\n");
		getch();
	}
	return;
}
void daydetect()
{
	int yr,mon,day,n,day1;
	char mons[10];
	lab3:
	printf("Enter the year in digits: ");
	scanf("%d",&yr);
	if(yr<1)
	{
		printf("Please provide a correct input for year.\n");
		printf("The value should not be less than 1.\n");
		goto lab3;
	}
	printf("Enter the first three characters of the month: ");
	scanf("%s",&mons);
	mon=calcmonth(mons);
	printf("Enter the day in digits: ");
	scanf("%d",&day);
	if(day<1||day>31)
	{
		printf("Please provide a correct input for day.");
		printf("\nThe value should only be between 1 and 31\n");
		goto lab3;
	}
	n=funcday(yr,mon);
	day1=(day+n)%7;
	switch(day1)
	{
		case 1: printf("\n\t***%dth %s %d is a Sunday***",day,mons,yr);
			break;
		case 2: printf("\n\t***%dth %s %d is a Monday***",day,mons,yr);
			break;
		case 3: printf("\n\t***%dth %s %d is a Tuesday***",day,mons,yr);
			break;
		case 4: printf("\n\t***%dth %s %d is a Wednesday***",day,mons,yr);
			break;
		case 5: printf("\n\t***%dth %s %d is a Thursday***",day,mons,yr);
			break;
		case 6: printf("\n\t***%dth %s %d is a Friday***",day,mons,yr);
			break;
		case 0: printf("\n\t***%dth %s %d is a Saturday***",day,mons,yr);
			break;
	}
	return;
}
int funcday(int yr,int mon)
{
	int a[15]={31,28,31,30,31,30,31,31,30,31,30,31};
	int i,cd,g,w;
	long int ab=0;
	int defyr=1;
	while(yr!=defyr)
	{
		if(defyr%100==0)
			w=400;
		else
			w=4;
		if((defyr%w==0))
		{
			ab=ab+366;
		}
		else
		{
			ab=ab+365;
		}
		defyr=defyr+1;
	}
	if(yr==defyr)
	{
		for(i=0;i<(mon-1);i++)
		{
			ab=ab+a[i];
		}
		if((yr%w==0)&&(mon>2))
			ab=ab+1;
	}
	cd=(ab+1)%7;
	return cd;
}
int funcno(int yr,int mon)
{
	int r;
	switch(mon)
	{
		case 1: r=31;
			break;
		case 2: if(yr%4==0)
				r=29;
			else
				r=28;
			break;
		case 3: r=31;
			break;
		case 4: r=30;
			break;
		case 5: r=31;
			break;
		case 6: r=30;
			break;
		case 7: r=31;
			break;
		case 8: r=31;
			break;
		case 9: r=30;
			break;
		case 10: r=31;
			break;
		case 11: r=30;
			break;
		case 12: r=31;
			break;
	}
	return r;
}
void dispmonthname(int mon,int yr)
{
	if(mon==1)
		printf("\n\t\t *****JANUARY %d*****\n\n",yr);
	else if(mon==2)
		printf("\n\t\t *****FEBRUARY %d*****\n\n",yr);
	else if(mon==3)
		printf("\n\t\t *****MARCH %d*****\n\n",yr);
	else if(mon==4)
		printf("\n\t\t *****APRIL %d*****\n\n",yr);
	else if(mon==5)
		printf("\n\t\t *****MAY %d*****\n\n",yr);
	else if(mon==6)
		printf("\n\t\t *****JUNE %d*****\n\n",yr);
	else if(mon==7)
		printf("\n\t\t *****JULY %d*****\n\n",yr);
	else if(mon==8)
		printf("\n\t\t *****AUGUST %d*****\n\n",yr);
	else if(mon==9)
		printf("\n\t\t *****SEPTEMBER %d*****\n\n",yr);
	else if(mon==10)
		printf("\n\t\t *****OCTOBER %d*****\n\n",yr);
	else if(mon==11)
		printf("\n\t\t *****NOVEMBER %d******\n\n",yr);
	else if(mon==12)
		printf("\n\t\t *****DECEMBER %d*****\n\n",yr);
	return;
}

int calcmonth(char mon1)
{
	int n;
	if(strcmp(mon1,"jan")==0)
		n=1;
	else if(strcmp(mon1,"feb")==0)
		n=2;
	else if(strcmp(mon1,"mar")==0)
		n=3;
	else if(strcmp(mon1,"apr")==0)
		n=4;
	else if(strcmp(mon1,"may")==0)
		n=5;
	else if(strcmp(mon1,"jun")==0)
		n=6;
	else if(strcmp(mon1,"jul")==0)
		n=7;
	else if(strcmp(mon1,"aug")==0)
		n=8;
	else if(strcmp(mon1,"sep")==0)
		n=9;
	else if(strcmp(mon1,"oct")==0)
		n=10;
	else if(strcmp(mon1,"nov")==0)
		n=11;
	else if(strcmp(mon1,"dec")==0)
		n=12;
	else
	{
		printf("Please provide the correct value for month...\n");
		printf("Try again from first\n");
		getch();
		exit(0);
	}
	return n;
}



Mod edit - Please :code:
Was This Post Helpful? 0
  • +
  • -

#4 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 162
  • View blog
  • Posts: 1,409
  • Joined: 13-June 08

Re: Calendar

Posted 17 July 2009 - 06:27 AM

use code tags and please don't double post.

Also, I know there's a mathematical formula for determining the first day of every month.

This post has been edited by ccubed: 17 July 2009 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 jjsaw5  Icon User is offline

  • I must break you
  • member icon

Reputation: 90
  • View blog
  • Posts: 3,060
  • Joined: 04-January 08

Re: Calendar

Posted 17 July 2009 - 06:48 AM

:code:
Was This Post Helpful? 0
  • +
  • -

#6 wildgoose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 468
  • Joined: 29-June 09

Re: Calendar

Posted 17 July 2009 - 10:39 AM

Yes please, use the tags.
to big for me to spend time formatting my self in an editor just so I can read it!
AND include whitespace between code blocks!


I can't tell from your post since I can't read your code as posted. Are you handling leap years so every 4 years, except every 100 years, except every 400 years?



To speed up your function don't do this
int a[15]={31,28,31,30,31,30,31,31,30,31,30,31};



do this
int a[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };



I used base 0, while you used base 1, but the logic still applies!

int funcday(int yr, int mon, int day)
{
   int d = a[ mon ] + day;
   if (mon >= 2)
   { 
		d += (int)isLeapYear( yr );
	}

   return d;
}

int NumDaysInMonth( int yr, int mon )
{
	 int d = a[ mon + 1 ] - a[ mon ];	// subtract to get range
	 if (mon == 1 && isLeapYear( yr ))
	 {
		 d++;
	 }

	 return d;
}



Also buils a string arrays.

char szMon[] = { "Jan", "Feb", "Mar", ...  , "Nov", "Dec" };



takes much less space and easier to read!

This post has been edited by wildgoose: 17 July 2009 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 mono15591  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 406
  • Joined: 05-November 08

Re: Calendar

Posted 17 July 2009 - 11:52 AM

Are you remembering to add a day every leap year?
nm I see your 4th year check now

This post has been edited by mono15591: 17 July 2009 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1