Monday 22 October 2012

Function038

Write a C program to accept a date in the format DD / MM / YYYY and add an integer to get the resultant date.

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

int leap ( int    yy )
{        
     if ( yy % 400 == 0 || ( yy % 100 != 0 && yy % 4 == 0 ) )
                   return 1;
     return 0;
}         
                
void add_day ( int    dd, int    mm, int    yy, int    day )
{             
     int    days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     dd = dd + day;
     if ( leap ( yy ) )
               days[2] = 29;
     else                  
               days[2]=28;
     while ( dd > days[mm] )
     {                               
               if ( dd > days[mm] )
               {                           
                         dd = dd - days[mm];
                         mm++;
               }                       
               if ( mm > 12 )
               {                                                
                         mm = 1;
                         yy = yy + 1;
                         if ( leap ( yy ) )
                                    days[2] = 29;
                         else               
                                    days[2]=28;
               }
     }
     printf("\n\n\n\t The resultant date is..:%d/%d/%d",dd,mm,yy);
}                     
                   
void main ( )
{
      int         dd, mm, yy, day;
      clrscr ( );
           
      printf("\n\n\n\t Enter the date in (DD/MM/YYYY) format:");
      scanf("%d/%d/%d",&dd,&mm,&yy);
      printf("\n\t Enter the number of days you want to add..:"); scanf("%d",&day);
      add_day ( dd, mm, yy, day );
      getch ( );
}
Click here to contact us 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...