Thursday, August 23, 2012

Miscellaneous – VII – Date Related Programs



Some Date Manipulation program and a program for generating Calendar.



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

//Print the day of the week from given date

  int valid(long dd,long mm,long yy,int mn[])
      {
           if(mm>12 || mm<1)
              {
                   return 1;
              }
           if(yy<1)
              {
                   return 1;
              }
           if(dd>mn[mm-1])
              {
                   return 1;
              }
           return 0;
      }

  void main()
      {
           char days[8][12]={"Monday","Tusesday","Wednesday","Thrusday","Friday","Saturday","Sunday"};
           long m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           long dd,mm,yy,i,d,td=0;
           clrscr();
           printf("\n\tEnter Day : ");
           scanf("%ld",&dd);
           printf("\n\tEnter Month : ");
           scanf("%ld",&mm);
           printf("\n\tEnter  Year : ");
           scanf("%ld",&yy);
           i=valid(dd,mm,yy,m);
           if(i==1)
           {
              printf("\n\n\t%ld-%ld-%ld is an invalid Date ",dd,mm,yy);
              getch();
              exit(0);
           }
           printf("\n\tWeek day for Date %2ld/%2ld/%4ld is ",dd,mm,yy);
           if(yy>1900)
              {
                   td=((yy-1)-1900)*365+((yy-1)-1900)/4;
              }
           if(yy==1900)
              {
                   td=td+dd-1;
              }
           else
              {
                   td=td+dd;
              }
           for(i=1;i<mm;i++)
              {
                   if(yy%4==0)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   td=td+m[i-1];
              }
           d=td%7;
           printf(" %s ",days[d]);
           getch();
      }





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

//Program for Generating Calendar

void main()
      {
           char b[12][12]={"January","February","March","April","May","June","July","August","September","October","November","December"};
           char a[8][4]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
           long m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           long y,i,d,j,td=0,k,n;
           clrscr();
           printf("\n\tEnter  Year : ");
           scanf("%ld",&y);
           td=((y-1)-1900)*365+((y-1)-1900)/4;
           d=(td+1)%7;
           printf("\n\n\t\tCalender of Year : %d ",y);
           n=d;
           for(i=0;i<12;i++)
              {
                   if(y%4==0)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   printf("\n\n\t\tMonth : %s \n\n",b[i]);
                   for(j=0;j<7;j++)
                        {
                            printf("   %s\t",a[j]);
                        }
                   k=0;
                   printf("\n",n);
                   for(j=1;j<=n;j++)
                        {
                            printf("\t");
                        }
                   while(k<m[i])
                        {
                            k++;
                            n++;
                            printf("   %2d \t",k);
                            if(n==7)
                                {
                                      n=0;
                                      printf("\n");
                                }
                        }
                   getch();
              }
           getch();
      }

No comments: