Program tells you the day of the date inputed

Discussion in 'C' started by shabbir, Apr 29, 2007.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    Program tells you the day of the date inputed. Remember when you copy the code paste in a file named *.c and not cpp file as that will cause an error. I have commented in the code where it will cause the error.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<time.h>
    
    #define AND &&
    #define OR ||
    #define RD 1900  /*Reference date 1900 jan 1st monday*/
    
    
    const int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
    /* * *  Program tells you the day of the date inputed  * * */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 
    
    void EnterDate(int *dt)
    {
    	do
    	{
    		if(*dt == 0)
    			printf("\nENTER YOUR DATE OF BIRTH\n");
    		printf("\nENTER DATE\n");
    		scanf("%d",dt);
    		if((*dt) <= 0 OR (*dt) > 31)
    		{
    			printf("\nSORRY NO ILLEGAL ENTRIES PLEASE\n");
    			printf("\nDATE OF ANY MONTH CANNOT EXCEED 31 OR GO BEYOND 0\n");
    			printf("\nENTER AGAIN\n");
    		}
    		else
    		{
    			break;
    		}
    	}while (1);
    }
    
    void EnterMonth(int *dt, int *m)
    {
    	do
    	{
    		printf("\nENTER MONTH \n");
    		scanf("%d",m);
    		if(*m <= 0 OR *m > 12)
    		{
    			printf("\nTHERE ARE TWELVE MONTH'S IN AN YEAR\n");
    			printf("\nENTER AGAIN\n");
    			continue;
    		}
    		if((*dt) > month[*m-1] AND *m != 2)
    		{ 
    			printf("\nTHIS MONTH DOES NOT HAVE THIS DATE \n");
    			printf("\nENTER AGAIN\n");
    			continue;
    		}
    		if(*dt == 30 AND *m == 2)
    		{
    			printf("FEBRUARY HAS A MAXIMUM OF 29 DAYS");
    			printf("\nENTER AGAIN\n");
    			EnterDate(dt);
    			continue;
    		}
    		break;
    	}while(1);
    }
    
    void EnterYear(int *dt, int *m, int *yr)
    {
    	do
    	{
    		printf("\nENTER YEAR \n");
    		scanf("%d",yr);
    		if(*yr<1900)
    		{
    			printf("\nI DON'T THINK YOU ARE THIS OLD\n");
    			printf("\nENTER AGAIN\n");
    			continue;
    		}
    		if(*m==2 AND *dt==29)
    		{
    			if((*yr%4==0)AND((*yr%100!=0)OR(*yr%400==0)))
    				printf("\nYOU ARE LUCKY TO HAVE SUCH A UNIQUE BIRTH DATE\n");
    			else
    			{
    				printf("\nFEBRUARY IN THE YEAR %d HAS 28 DAYS\n",*yr);
    				printf("\nENTER AGAIN\n");
    				continue;
    			}
    		}
    		break;
    	}while(1);
    }
    
    int main()
    {
    	int m = 0,dt = 0,yr = 0,i = 0;
    	unsigned int ydays,days,leap=0,mdays=0,ddays;
    	char ch;
    	EnterDate(&dt);
    	EnterMonth(&dt,&m);
    	EnterYear(&dt,&m,&yr);
    
    	for(i=RD;i<yr;i++)
    	{
    		if((i%4==0)AND((i%100!=0)OR(i%400==0)))
    			leap++;
    	}
    	for(i=0;i<(m-1)AND(i<11);i++)
    		mdays=mdays+month[i];
    	if(m>2  AND (yr%4==0) AND ((yr%100!=0)OR(yr%400==0)))
    		mdays++;
    	ydays=(yr-RD)*365;
    	ddays=dt-1;
    	days=ydays+leap+mdays+ddays;
    	switch (days%7)
    	{
    	case 0:
    		printf("\nYOU WERE BORN ON MONDAY\n");
    		break;
    	case 1:
    		printf("\nYOU WERE BORN ON TUESDAY\n");
    		break;
    	case 2:
    		printf("\nYOU WERE BORN ON WEDNESDAY\n");
    		break;
    	case 3:
    		printf("\nYOU WERE BORN ON THURSDAY\n");
    		break;
    	case 4:
    		printf("\nYOU WERE BORN ON FRIDAY\n");
    		break;
    	case 5:
    		printf("\nYOU WERE BORN ON SATURDAY\n");
    		break;
    	case 6:
    		printf("\nYOU WERE BORN ON SUNDAY\n");
    		break;
    	}
    	printf("\nDO YOU WISH TO CONTINUE[Y/N]\n");
    	fflush(stdin);
    	scanf("%c",&ch);
    	if(ch=='Y' OR ch=='y')
    	{
    		// This is possible in C but with .cpp as file name you will have errors
    		main();
    	}
    	return 0;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    [thread=4074]Month calender in plain C[/thread] tells you the complete month details and display it as in any standard calender.
     
  3. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Nice article. Btw. the CPP issue can be fixed using "goto" :)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice