Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Program tells you the day of the date inputed (http://www.go4expert.com/articles/program-tells-day-date-inputed-t4073/)

shabbir 29Apr2007 16:25

Program tells you the day of the date inputed
 
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: C

#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;
}


shabbir 29Apr2007 18:51

Re: Program tells you the day of the date inputed
 
Month calender in plain C tells you the complete month details and display it as in any standard calender.

Scripting 18Feb2012 20:28

Re: Program tells you the day of the date inputed
 
Nice article. Btw. the CPP issue can be fixed using "goto" :)


All times are GMT +5.5. The time now is 01:51.