Saturday, January 30, 2010

CHAP 2[C]d Finding day on first of Jan on a particular year

According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

void main()
{
float days;
int year,diff,leap,type;
long int days1;
printf("\nInput the year");
scanf("%d",&year);
year=year-1;
diff=year-1900;

/* The line year=year-1 was written because we are finding the days before
that particular year not that full year as the required date is 01/01/year.

In days... 365 was added because the year 1900 has to be taken into account
as 1900-1904 is not 1904-1900=4years but is 5 years. 1900 is not a leap year.

In days... addition of one was added because the first day of the year
(1st of Jan) has to be accounted for*/

if(diff<100)
{
leap =diff/4;
days=(366.0*leap)+((diff-leap)*365+365+1);
days1=days;
type=days1%7;
}

if(diff>=100)
{
leap = (diff/4)-(diff/100)+1+((year-2000)/400);
days=(366.0*leap)+((diff-leap)*365+365+1);
days1=days;
type=days1%7;
}

if(type==0)
printf("Sunday");
if(type==1)
printf("Monday");
if(type==2)
printf("Tuesday");
if(type==3)
printf("Wednesday");
if(type==4)
printf("Thursday");
if(type==5)
printf("Friday");
if(type==6)
printf("Saturday");

printf("\nThe leap is %d\nThe days is %f\nThe type is %d",leap,days,type);

}

12 comments:

  1. Hi there, I am new to 'C'. learning it myself with the help of the book 'LET US C'.
    could you explain me the output of the following problem out of chapter two [e](c)
    main()
    {
    int i=4,j=-1,k=0,w,x,y,z;
    w=i||j||k;
    x=i&&j&&k;
    y=i||j&&k;
    z=i&&j||k;
    printf("\nw=%d x=%d y=%d z=%d",w,x,y,z);
    }

    thanks in advance

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. there is confusion of 1 day in program make it correct. 1901's 1 st is become wednesday and your answer is tuesday. thanks for help. every time the answer goes 1 day short.

    #include
    #include
    main()
    {
    int yr, days, diff, days1, leap, nonleap, type;

    printf("current year");
    scanf("%d", &yr);

    yr = yr-1;
    diff = yr-2012;
    leap = diff/4;
    nonleap= diff-leap;
    days= (365)+(leap*366)+(nonleap*365)+1;
    // days = days1;
    type = days%7;

    if(type==0)
    printf("the 1st day of this year is sunday");
    if(type==1)
    printf("the 1st day of this year monday");
    if(type==2)
    printf("the 1st day of this year tuesday");
    if(type==3)
    printf("the 1st day of this year wednesday");
    if(type==4)
    printf("the 1st day of this year thursday");
    if(type==5)
    printf("the 1st day of this year friady");
    if(type==6)
    printf("the 1st day of this year satday");
    getch();
    }

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. i have taken 1900 as 2012 for reference and simplicity, you can replace any leap year for 2012

    ReplyDelete
  7. the output of the program is not typing the day like monday or tues day,rather it is giving the type like 1,2, or 3..why is it so?

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. is this program correct
    or not? it was Monday
    on
    the date 01/01/2001.
    void main()
    {
    int year, diff, leap, i;
    printf("\nInput the
    year");
    scanf("%d",&year);
    diff=year-2001;
    leap=diff/4;
    i=diff+leap;
    if(diff>=100)
    i=i-1;
    i=i%7;
    if(i==6)
    printf("Sunday");
    if(i==0)
    printf("Monday");
    if(i==1)
    printf("Tuesday");
    if(i==2)
    printf("Wednesday");
    if(i==3)
    printf("Thursday");
    if(i==4)
    printf("Friday");
    if(i==5)
    printf("Saturday");
    }

    ReplyDelete
  10. hey ur program is working correctly ,cn u xplain in detail if the diff is > 100 part please,especially calculation of no.of leap years ,it would be great

    ReplyDelete
  11. easiest -
    #include
    #include
    main()
    {
    int y,i,s=0;
    clrscr();
    printf("enter year = ");
    scanf("%d",&y);
    for(i=1900;i<y;i++)
    {
    if(i%400==0)
    s=s+366;
    else if(i%100==0 && i%400!=0)
    s=s+365;
    else if(i%4==0 && i%100!=0)
    s=s+366;
    else if (i%4!=0)
    s=s+365;
    }
    if(s%7==0)
    printf("monday");
    else if(s%7==1)
    printf("tuesday");
    else if(s%7==2)
    printf("wednesday");
    else if(s%7==3)
    printf("thursday");
    else if(s%7==4)
    printf("friday");
    else if(s%7==5)
    printf("saturday");
    else if(s%7==6)
    printf("sunday");

    getch();

    }

    ReplyDelete