Computer Science, asked by bipandeep3140, 1 year ago

Write a c program to create a structure called date with day, month and year as data member and to find the difference between two dates

Answers

Answered by p1998
4
/* Define two object of date which structure type and
find difference between two date */
#include<stdio.h>
#include<conio.h>
struct dt
{
int dd;
int mm;
int yy;
};
typedef dt date;
void main()
{
date dob,c_date,today;
clrscr();
printf("Enter Date of birth :\n");
printf("Day :");scanf("%d",&dob.dd);
printf("Month :");scanf("%d",&dob.mm);
printf("Year :");scanf("%d",&dob.yy);
printf("\n");
printf("Enter Current date :\n");
printf("Day :");scanf("%d",&c_date.dd);
printf("Month :");scanf("%d",&c_date.mm);
printf("Year :");scanf("%d",&c_date.yy);
/* Diff of two date */
if(c_date.dd>=dob.dd)
today.dd = c_date.dd-dob.dd;
else
{
c_date.dd+=30;
c_date.mm-=1;
today.dd = c_date.dd-dob.dd;
}
if(c_date.mm>=dob.mm)
today.mm = c_date.mm-dob.mm;
else
{
c_date.mm+=12;
c_date.yy-=1;
today.mm = c_date.dd-dob.mm;
}
today.yy = c_date.yy-dob.yy;
printf("Current date is : ");
printf("%2d-%2d-%4d\n\n",c_date.dd,c_date.mm,c_date.yy);
printf("Date of Birth : ");
printf("%2d-%2d-%4d\n\n",dob.dd,dob.mm,dob.yy);
printf("Today your age: \n");
printf("Day : %d\n",today.dd);
printf("Month : %d\n",today.mm);
printf("Year : %d\n",today.yy);
getch();
}


Answered by sarahssynergy
0

Program to create a structure called date with day, month and year as data member and to find the difference between two dates

Explanation:

#include<stdio.h> // include stdio.h library

#include<stdlib.h> // include stdlib.h library

int valid_date(int date, int mon, int year);

int main(void)

{

   int day1, mon1, year1,

       day2, mon2, year2;

   int day_diff, mon_diff, year_diff;        

   printf("Enter start date (MM/DD/YYYY): ");

   scanf("%d/%d/%d", &mon1, &day1, &year1);

   printf("Enter end date (MM/DD/YYYY): ");

   scanf("%d/%d/%d", &mon2, &day2, &year2);

   if(!valid_date(day1, mon1, year1))

   {

       printf("First date is invalid.\n");        

   }

   if(!valid_date(day2, mon2, year2))

   {

       printf("Second date is invalid.\n");

       exit(0);

   }      

   if(day2 < day1)

   {      

       // borrow days from february

       if (mon2 == 3)

       {

           //  check whether year is a leap year

           if ((year2 % 4 == 0 && year2 % 100 != 0) || (year2 % 400 == 0))

           {

               day2 += 29;

           }

           else

           {

               day2 += 28;

           }                        

       }

       // borrow days from April or June or September or November

       else if (mon2 == 5 || mon2 == 7 || mon2 == 10 || mon2 == 12)

       {

          day2 += 30;

       }

       // borrow days from Jan or Mar or May or July or Aug or Oct or Dec

       else

       {

          day2 += 31;

       }

       mon2 = mon2 - 1;

   }

   if (mon2 < mon1)

   {

       mon2 += 12;

       year2 -= 1;

   }      

   day_diff = day2 - day1;

   mon_diff = mon2 - mon1;

   year_diff = year2 - year1;

   printf("Difference: %d years %02d months and %02d days.", year_diff, mon_diff, day_diff);

   return 0; // return 0 to operating system

}

// function to check whether a date is valid or not

int valid_date(int day, int mon, int year)    

{

   int is_valid = 1, is_leap = 0;

   if (year >= 1800 && year <= 9999)

   {

       //  check whether year is a leap year

       if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

       {

           is_leap = 1;

       }

       // check whether mon is between 1 and 12

       if(mon >= 1 && mon <= 12)

       {

           // check for days in feb

           if (mon == 2)

           {

               if (is_leap && day == 29)

               {

                   is_valid = 1;

               }

               else if(day > 28)

               {

                   is_valid = 0;

               }

           }

           // check for days in April, June, September and November

           else if (mon == 4 || mon == 6 || mon == 9 || mon == 11)

           {

               if (day > 30)

               {

                   is_valid = 0;

               }

           }

// check for days in rest of the months

           // i.e Jan, Mar, May, July, Aug, Oct, Dec

           else if(day > 31)

           {            

               is_valid = 0;

           }        

       }

       else

       {

           is_valid = 0;

       }

   }

   else

   {

       is_valid = 0;

   }

   return is_valid;

}

Similar questions