Thursday, August 16, 2012

Miscellaneous – VI – Date Related Programs



Some Date Manipulation programs.




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

//Find Date after Numbers of Days

  int Leap(int y)
      {
           if(y%100!=0 && y%4==0)
              {
                   return 1;
              }
           if(y%100==0 && y%400==0)
              {
                   return 1;
              }
           return 0;
      }

  int valid(int dd,int mm,int 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()
      {
           int m[12]={31,28,31,30,31,30,31,31,30,31,30,31};
           int dd,mm,yy,n,c;
           clrscr();
           printf("\n\tEnter Day : ");
           scanf("%d",&dd);
           printf("\n\tEnter Month : ");
           scanf("%d",&mm);
           printf("\n\tEnter  Year : ");
           scanf("%d",&yy);
           c=valid(dd,mm,yy,m);
           if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid Date ",dd,mm,yy);
              getch();
              exit(0);
           }
           printf("\n\tEnter Nos. of Days to Add : ");
           scanf("%d",&n);
           printf("\n\tInput Date %2d/%2d/%4d is ",dd,mm,yy);
           printf("\n\tDate after %d Days ",n);
           while(n>0)
              {
                   c=Leap(yy);
                   if(c==1)
                        {
                            m[1]=29;
                        }
                   else
                        {
                            m[1]=28;
                        }
                   n--;
                   dd++;
                   if(dd>m[mm-1])
                        {
                            mm++;
                            dd=1;
                        }
                   if(mm>12)
                        {
                            yy++;
                            mm=1;
                        }
              }
           printf("  %d-%d-%d",dd,mm,yy);
           getch();
      }
 

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

//Find the Difference between two dates

  int valid(int dd,int mm,int 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()
   {
       int d,m,y,d1,m1,y1,d2,m2,y2,c;
       int mn[]={31,28,31,30,31,30,31,31,30,31,30,31};
       clrscr();
       printf("\nEnter the First Date dd mm yyyy : ");
       scanf("%d%d%d",&d,&m,&y);
       c=valid(d,m,y,mn);
       if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid date ",d,m,y);
              getch();
              exit(0);
           }
       printf("\nEnter the Second Date dd mm yyyy : ");
       scanf("%d%d%d",&d2,&m2,&y2);
       c=valid(d2,m2,y2,mn);
       if(c==1)
           {
              printf("\n\n\t%d-%d-%d is an invalid date ",d2,m2,y2);
              getch();
              exit(0);
           }

       printf("\n\First Date : %2d-%2d-%4d ",d,m,y);
       printf("\n\t\tSecond Date : %2d-%2d-%4d ",d2,m2,y2);
       if(y>y2)
           {
              printf("\nInvaid Input ");
              getch();
              exit(0);
           }

       d1=d2-d;
       if(d1<0)
           {
              d1=mn[m-1]-d;
              d1=d1+d2;
              m=m+1;
           }
       m1=m2-m;
       if(m1<0)
           {
              m1=12-m;
              m1=m1+m2;
              y=y+1;
           }

       y1=y2-y;
       if(y1<0)
       y1=0;
       printf("\n\t\t\t Difference = %d years %d months and %d days ",y1,m1,d1);
       getch();
  }

  

No comments: