Tuesday, March 22, 2011

Structures and Unions

Use the date structure defined in previous program to store two dates. Develop a function that will take these two dates as input and compares them.
  • It returns 1, if the date1 is earlier than date2
  • It returns 0, if the date1 is later date
#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
void compare(struct date date1,struct date date2);
void main()
{
struct date date1,date2;
clrscr();
printf("Enter first date dd mm yyyy:");
scanf("%d %d %d",&date1.day,&date1.month,&date1.year);
printf("Enter Second date dd mm yyyy:");
scanf("%d %d %d",&date2.day,&date2.month,&date2.year);
compare(date1,date2);
getch();
}
void compare(struct date date1,struct date date2)
{
if(date1.year<date2.year)
printf("1");
if(date1.year>date2.year)
printf("0");
if(date1.year==date2.year)
{
if(date1.month<date2.month)
printf("1");
if(date1.month>date2.month)
printf("0");
if(date1.month==date2.month)
{
if(date1.day<date2.day)
printf("1");
if(date1.day>date2.day)
printf("0");
}
}
}

OUTPUT
Enter first date dd mm yyyy: 25 2 2010
Enter Second date dd mm yyyy: 25 2 2011
1

Enter fist date dd mm yyyy: 25 2 2000
Enter Second date dd mm yyyy: 24 2 2000
0

No comments:

Post a Comment