Friday 23 March 2012

Program in c to display calendar corresponding to given year


Program in c to display calendar corresponding to given year.
                    
#include<stdio.h>
#include<conio.h>
int month(int,int);
void main()
 {
  int yy,a,a1,a2,b,b1,b2,i,sm=0,c,d,e;
  clrscr();
  printf("Enter  which year you want show the calender-" );
  scanf("%d",&yy);
  yy=yy-1;
  a=yy%400;
  b=yy/400;
  a1=a%100;
  b1=a/100;
  a2=a1/4;
  b2=a1-a2;
  sm=b*0+b1*5+a2*2+b2*1+1;
  sm=sm%7;
  printf("\n\n\n\nJanuary\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nFebruary\n\n");
  if((yy+1)%4==0)
   sm=month(29,sm);
  else
   sm=month(28,sm);
  printf("\n\n\n\nMarch\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nApril\n\n");
  sm=month(30,sm);
  printf("\n\n\n\nMay\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nJune\n\n");
  sm=month(30,sm);
  printf("\n\n\n\nJuly\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nAugust\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nSeptember\n\n");
  sm=month(30,sm);
  printf("\n\n\n\nOctober\n\n");
  sm=month(31,sm);
  printf("\n\n\n\nNovember\n\n");
  sm=month(30,sm);
  printf("\n\n\n\nDecember\n\n");
  sm=month(31,sm);
  getch();
 }
int month(int c,int d)
 {
  int i,j,k;
  printf("   Sun   Mon   Tue   Wed   Thu   Fri   Sat\n\n");
  for(i=0;i<=d;i++)
  if(i>=d)
  {
   for(j=1;j<=c;j++)
   {
    if(i%7==0)
    printf("\n");
    printf("%6d",j);
    i++;
   }
  }
  else
   printf("      ");
  k=(c+d)%7;
  return(k);
 }
output:

Enter which year you want show the calander-2012

January

  Sun  Mon  Tue  Wed  Thu  Fri  Sat


      1     2      3      4      5      6     7
     8     9      10    11    12    13    14
    15    16    17    18    19    20    21
    22    23    24    25    26    27    28
    29    30    31


February

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                        1     2      3     4
     5      6       7      8     9     10    11
    12    13    14    15    16    17    18
    19    20    21    22    23    24    25
    26    27    28    29


March

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                                       1     2     3
      4     5       6      7      8      9     10
     11    12    13    14    15    16    17
     18    19    20    21    22    23    24
     25    26    27    28    29    30    31


April

   Sun  Mon  Tue  Wed  Thu  Fri  Sat


      1      2      3       4       5      6     7
      8      9     10     11     12     13   14
     15     16   17     18     19     20   21
     22     23   24     25     26     27   28
     29     30


May

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                           1       2       3      4     5
     6      7       8       9       10    11   12
    13    14     15     16      17    18   19

    20    21    22    23    24    25    26
    27    28    29    30    31
June

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                                                    1     2
     3      4      5       6        7      8     9
    10    11    12     13     14     15    16
    17    18    19     20     21     22    23
    24    25    26     27     28     29    30


July

   Sun  Mon  Tue  Wed  Thu  Fri  Sat


     1      2      3       4       5       6     7
     8      9     10     11     12     13    14
    15    16    17     18     19     20    21
    22    23    24     25     26     27    28
    29    30    31


August

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                        1       2       3     4
     5      6       7      8       9      10    11
    12    13     14    15     16     17    18
    19    20     21    22     23     24    25
    26    27     28    29     30     31


September

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                                                                1
     2      3      4       5       6       7      8
     9     10    11     12     13     14     15
    16    17    18    19    20    21    22
    23    24    25    26    27    28    29
    30


October

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

            1        2      3        4      5     6
     7     8        9     10       11    12   13
    14    15     16    17       18    19   20
    21    22     23    24       25    26   27
    28    29     30    31


November

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                    1      2     3
     4     5       6        7      8      9     10
    11    12    13       14    15    16   17
    18    19    20       21    22    23   24
    25    26    27       28    29    30


December

   Sun  Mon  Tue  Wed  Thu  Fri  Sat

                                                    1
     2      3       4      5       6       7     8
     9     10     11    12     13     14    15
    16    17     18    19     20     21    22
    23    24     25    26     27     28    29
    30    31

10 comments:

  1. sir i will check the coding but ths coding only give the output oif nov & dec. why?

    ReplyDelete
  2. # include
    # include
    main()
    {
    char m[10];
    int year,d=0,c=0,f,e,temp,day=1,z =0,len =0,i,j;



    printf("enter the year and month\n");
    scanf("%d %s",&year,&m);

    printf("the year is : %d ,month is : %s",year,m);

    temp = year;
    if(temp > 1600)
    {
    temp = temp - 1600;
    c =0;
    }
    if(temp>400)
    {
    temp = temp - 400;
    c = 0;
    }
    if(temp > 300)
    {
    temp=temp-300;
    c=1;
    }
    temp = temp-1;
    d = temp/4;
    printf("\n tha value of d: %d ",d);
    e = (d*2) + (temp - d);
    f = e %7;
    f = f+c;
    if(((year%4==0)&&(year%100!=0))||(year%400==0))
    {
    z = 1;
    }

    printf("\n the values are : \n %d ",f);
    if(strcmp(m,"JAN")==0)
    {
    day = 1;
    len = 31;
    }
    if(strcmp(m,"FEB")==0)
    {
    day = day + 31;
    len = 28 +z;
    }
    if(strcmp(m,"MAR")==0)
    {
    day = day +31+28 + z;
    len = 31;
    }
    if(strcmp(m,"APR")==0)
    {
    day = day + 31 + 28 + z + 31;
    len = 30;
    }
    if(strcmp(m,"MAY")==0)
    {
    day = day + 31 + 28 + z + 31 + 30;
    len =31;
    }
    if(strcmp(m,"JUN")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31;
    len = 30;
    }
    if(strcmp(m,"JUL")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 +30;
    len = 31;
    }
    if(strcmp(m,"AUG")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 + 30 +31;
    len = 31;
    }
    if(strcmp(m,"SEP")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 +30 +31 + 31;
    len = 30;
    }
    if(strcmp(m,"OCT")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 +30 +31 + 31 + 30;
    len = 31;
    }
    if(strcmp(m,"NOV")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 +30 +31 + 31 + 30 + 31;
    len = 30;
    }
    if(strcmp(m,"DEC")==0)
    {
    day = day + 31 + 28 + z + 31 + 30 + 31 +30 +31 + 31 + 30 + 31 + 30;
    len = 31;
    }


    day = day % 7;
    day = day +f;
    day = day %7;
    printf("\n the no of days are: \n %d",day);

    printf("\nSUN \t MON \t TUE \t WED \t THU \t FRI \t SAT\n");
    for(j=0;j<day;j++)
    {
    printf("\t");
    }
    for(i =1 ;i<=len;i++)
    {
    j++;
    printf(" %d\t",i);
    if(j %7 == 0)
    {
    printf("\n");
    }

    }




    }

    ReplyDelete
    Replies
    1. it is wrong because 'strcmp' in not declared.

      Delete
  3. sm=b*0+b1*5+a2*2+b2*1+1;
    please explain this

    ReplyDelete
  4. the above program is wrong
    the year 2100 is a leap year and it contains only 28 days

    ReplyDelete
    Replies
    1. 2100 is not leap year leap year should be perfectly divisible by 400 if it is in multiple of 100

      Delete
  5. Can anyone give me a correct code to print the calender and the output has to be like the months in 3 columns. It would be a great help. Thank you

    ReplyDelete
  6. This program is wrong. It cant display calander of 2015.

    ReplyDelete
  7. please explain the term a,a1,a2,b,b1,b2,c,int i,int c,int d,i,j,c,d what is the meaning of these words please explain me anybody

    ReplyDelete