DAY AND TIME
Problem Description
Define a structure date containing three integers -day,months and year. Write a program using functions to read data, to validate the data entered by the user and then print the date on the screen.
For example,if you enter 31/6/2007 is invalid as June does not has 31 days, similarly if you enter 25/13/2000 then it is invalid month as month ends with 12(1-12. jan to dec). The threshold value for year is 3000 (Invalid Year). Valid year: 0000-2999
Using the structure definition of the above program, write a function to increment that .Make sure that the incremented date is a valid date. Modify the above program to add a specific number of days to the given date.Write a function to compare two date variable.
Output:
33 12 1989 - Invalid Day
31 12 3000 - Invalid Year
31 13 1989 - Invalid Month
25 12 1989- New Date=26 12 1989
Mandatory:
1. Create a Structure as "Date"
2. Create three data members as date(int), month(int), year(int)
3. The structure variable for "Date" structure is "D"
Note: The structure variables, data members and structure name are CASE Sensitive.
Follow the same case mentioned in the mandatory
CODING ARENA
#include <stdio.h>
struct Date
{
int date,month,year;
}D;
int main()
{
scanf("%d%d%d",&D.date,&D.month,&D.year);
if(D.year>=0000 && D.year<=2999)
{
if(D.month>=1 && D.month<=12)
{
if((D.date>=1 && D.date<=31) && (D.month==1 || D.month==3 ||D.month==5 || D.month==7 ||D.month==8||D.month==10||D.month==12))
{
printf("\nNew Date=%d %d %d",D.date+1,D.month,D.year);
}
else if((D.date>=1 && D.date<=30) && (D.month==4||D.month==6||D.month==9||D.month==11))
{
printf("\nNew Date=%d %d %d",D.date,D.month,D.year);
}
else if((D.date>=1 && D.date<=28)&& D.month==2)
{
printf("\nNew Date=%d %d %d",D.date,D.month,D.year);
}
else if(D.date==29 && D.month==2 &&(D.year%400==0 ||(D.year%4==0 && D.year%100!=0)))
{
printf("\nNew Date=%d %d %d",D.date,D.month,D.year);
}
else
printf("\nInvalid Day");
}
else
printf("\nInvalid Month");
}
else
printf("\nInvalid Year");
return 0;
}
Test Case 1
Input (stdin)31 14 1989
Expected Output
Invalid Month
Test Case 2
Input (stdin)25 12 1989
Expected Output
New Date=26 12 1989
No comments:
Post a Comment