Tuesday 5 March 2013

DETERMINATION OF THE DAY OF THE WEEK THROUGH A SIMPLE C PROGRAM


 

Have you ever wondered what if you were given any future or past date and you were asked to calculate the day of week...how would you proceed to solve this crazy problem.
Here is a solution for this problem. I have made a program in C that enables you to enter a date, and calculates the day of the week.
Here are a few screen-shots, have a look at them.


 Now to confirm that my programs runs well and calculates the day of the week perfectly, here is a link that i would recommend you to visit.
Some of the screenshots form this website are adder here to confirm that my program runs perfectly well.
 

You can even run my C program for a future date as shown below.
Although it seems crazy stuff...no doubt, it clearly depicts the power of programming....
BEWARE:
If you go for a google search for this topic you will get a large number of such calculators that ensures you for a perfect answer, but this is not the scenario all the time.
Some of the websites that gives a wrong prediction are:
and surely you can find many more....

Just put these anomalies aside and have a look at my program...
Note that my code runs perfectly well in turbo cpp...just after small amendments you can have a code for any other compiler. 


/*****************************************************************************/

//program to find day of week for a given date....

#include<stdio.h>
#include<conio.h>

void main()
{
    long int d,m,y,d1,m1,y1,d2,m2,y2,tot,lavish,i;
    clrscr();
    printf("Program to find the day of week for a given date\n\n");
    d1=1;
    m1=1;
    y1=0;
    printf("Enter the date in dd mm yyyy format: \n");
    scanf("%ld%ld%ld",&d2,&m2,&y2);

    y=y2-y1;
    m=m2-m1;
    d=d2-d1;

    tot=y*365+m*30+d;

    if(m2>2)
    {
        for(i=y1;i<=y2;i++)
        {
            if(i%4==0)
                tot++;

            if(i%100==0)
                if(i%400!=0)
                    tot--;
        }
    }
    else
    {
        for(i=y1;i<y2;i++)
        {
            if(i%4==0)
                tot++;

            if(i%100==0)
                if(i%400!=0)
                    tot--;
        }

    }


    if(m2>m1)
    {
        for(i=m1;i<m2;i++)
        {
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
                tot++;

            if(i==2)
                tot=tot-2;
        }

    }

    lavish=tot%7;

    printf("\n\n");

    switch(lavish)
    {
        case 0:
            printf("SATURDAY");
            break;
        case 1:
            printf("SUNDAY");
            break;
        case 2 :
            printf("MONDAY");
            break;
        case 3  :
            printf("TUESDAY");
            break;
        case 4   :
            printf("WEDNESDAY");
            break;
        case 5    :
            printf("THRUSDAY");
            break;
        case 6     :
            printf("FRIDAY");
            break;
    }
    getch();
}
/**********************************************************************************/

2 comments:

  1. Nice one..
    one suggestion from my side.. it would be really nice if there are some comments in that describe each subpart of the code and the logic /algorithm .. making it further easier for the viewers...

    ReplyDelete
  2. yes...I was also thinking over the same issue...
    my upcoming codes will be with proper comments :)

    ReplyDelete