Sunday, September 14, 2014

compare two given dates using structure


Problem

Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month, and year. If the dates are equal then display message as "Equal" otherwise "Unequal".

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct date
{
 int date;
 int month;
 int year;
};
int main ()
{
 int i,f=0;  struct date d[2];  for(i=0;i<2;i++)
 {
  printf("\nEnter day for the %dth date\n",i+1);
  scanf("%d",&d[i].date);

  printf("\nEnter the month for the %dth date\n",i+1);
  scanf("%d",&d[i].month);

  printf("\nEnter the year for the %dth date\n",i+1);
  scanf("%d",&d[i].year);
 }
 if(d[0].date==d[1].date)
 {
  if(d[0].month==d[1].month)
  {
   if(d[0].year==d[1].year)
   {
    f=1;
   }
  }
 }
 if(f==1)
  printf("\nThe dates are equal");
 else
  printf("\nThe dates are not equal");

 printf ("\n\npress any key to close.");
 getch();
}

Sample Output

compare dates using structure

2 comments :

  1. Replies
    1. because of garbage value if you don't put may be you can get garbage value

      Delete

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact