Define a structure data type named date containing three integer members day, month and year. Develop an interactive modular program to perform the following tasks:
by a third function.
The input data should be three integers like 29,4, and 2002 corresponding to day, month and year. Example of invalid data:
31, 4, 2002 - April has only 30 days
29, 2, 2002 - 2002 is not a leap year
#include <stdio.h>
struct date
{
int day;
int month;
int year;
}dat;
input();
void validate(struct date dat);
void print(struct date dat);
void main()
{
clrscr();
printf("Enter day:");
dat.day=input();
printf("Enter month:");
dat.month=input();
printf("Enter year:");
dat.year=input();
validate(dat);
getch();
}
input()
{
int a;
scanf("%d",&a);
return(a);
}
void validate(struct date dat)
{
if(dat.day>31||dat.day<1)
printf("The day should 1 to 31!");
else
if(dat.month>12||dat.month<1)
printf("Month should be 1 to 12 only!");
else
if(dat.month==2)
{
if(dat.day==29)
{
if(dat.year%4!=0)
printf("%d is not a leap year",dat.year);
else
print(dat);
}
else
if(dat.day>29)
printf("February has only 28 or 29 days");
else
print(dat);
}
else
if(dat.month==4)
{
if(dat.day>30)
printf("April has only 30 days");
}
else
if(dat.month==6)
{
if(dat.day>30)
printf("June has only 30 days");
}
else
if(dat.month==9)
{
if(dat.day>30)
printf("September has only 30 days");
}
else
if(dat.month==11)
{
if(dat.day>30)
printf("November has only 30 days");
}
else
print(dat);
}
void print(struct date dat)
{
switch(dat.month)
{
case 1:printf("January %d, %d",dat.day,dat.year);
break;
case 2:printf("February %d, %d",dat.day,dat.year);
break;
case 3:printf("March %d, %d",dat.day,dat.year);
break;
case 4:printf("April %d, %d",dat.day,dat.year);
break;
case 5:printf("May %d, %d",dat.day,dat.year);
break;
case 6:printf("June %d, %d",dat.day,dat.year);
break;
case 7:printf("July %d, %d",dat.day,dat.year);
break;
case 8:printf("August %d, %d",dat.day,dat.year);
break;
case 9:printf("September %d, %d",dat.day,dat.year);
break;
case 10:printf("October %d, %d",dat.day,dat.year);
break;
case 11:printf("November %d, %d",dat.day,dat.year);
break;
case 12:printf("December %d, %d",dat.day,dat.year);
break;
}
}
OUTPUT
Enter day: 5
Enter month: 2
Enter year: 2010
February 5, 2010
Enter day: 31
Enter month: 4
Enter year: 2010
April has only 30 days
Enter day: 29
Enter month: 2
Enter year: 2002
2002 is not a leap year
- To read data into structure members by a function
- To validate the date entered by another function
- To print the date in the format
by a third function.
The input data should be three integers like 29,4, and 2002 corresponding to day, month and year. Example of invalid data:
31, 4, 2002 - April has only 30 days
29, 2, 2002 - 2002 is not a leap year
#include <stdio.h>
struct date
{
int day;
int month;
int year;
}dat;
input();
void validate(struct date dat);
void print(struct date dat);
void main()
{
clrscr();
printf("Enter day:");
dat.day=input();
printf("Enter month:");
dat.month=input();
printf("Enter year:");
dat.year=input();
validate(dat);
getch();
}
input()
{
int a;
scanf("%d",&a);
return(a);
}
void validate(struct date dat)
{
if(dat.day>31||dat.day<1)
printf("The day should 1 to 31!");
else
if(dat.month>12||dat.month<1)
printf("Month should be 1 to 12 only!");
else
if(dat.month==2)
{
if(dat.day==29)
{
if(dat.year%4!=0)
printf("%d is not a leap year",dat.year);
else
print(dat);
}
else
if(dat.day>29)
printf("February has only 28 or 29 days");
else
print(dat);
}
else
if(dat.month==4)
{
if(dat.day>30)
printf("April has only 30 days");
}
else
if(dat.month==6)
{
if(dat.day>30)
printf("June has only 30 days");
}
else
if(dat.month==9)
{
if(dat.day>30)
printf("September has only 30 days");
}
else
if(dat.month==11)
{
if(dat.day>30)
printf("November has only 30 days");
}
else
print(dat);
}
void print(struct date dat)
{
switch(dat.month)
{
case 1:printf("January %d, %d",dat.day,dat.year);
break;
case 2:printf("February %d, %d",dat.day,dat.year);
break;
case 3:printf("March %d, %d",dat.day,dat.year);
break;
case 4:printf("April %d, %d",dat.day,dat.year);
break;
case 5:printf("May %d, %d",dat.day,dat.year);
break;
case 6:printf("June %d, %d",dat.day,dat.year);
break;
case 7:printf("July %d, %d",dat.day,dat.year);
break;
case 8:printf("August %d, %d",dat.day,dat.year);
break;
case 9:printf("September %d, %d",dat.day,dat.year);
break;
case 10:printf("October %d, %d",dat.day,dat.year);
break;
case 11:printf("November %d, %d",dat.day,dat.year);
break;
case 12:printf("December %d, %d",dat.day,dat.year);
break;
}
}
OUTPUT
Enter day: 5
Enter month: 2
Enter year: 2010
February 5, 2010
Enter day: 31
Enter month: 4
Enter year: 2010
April has only 30 days
Enter day: 29
Enter month: 2
Enter year: 2002
2002 is not a leap year
No comments:
Post a Comment