Question

Question 2: WRITE A C PROGRAMME TO Accept dates in ‘dd-mm-yyyy’ format into string variables and...

Question 2: WRITE A C PROGRAMME TO Accept dates in ‘dd-mm-yyyy’ format into string variables and perform the following

  1. Validate the given date
  2. Find the difference between two dates
  3. Find whether date d1 is earlier than or later than or equal to the date d2

Total Marks : 35 USE THE FOLLOWING GUIDLINES

String usage

10 marks

Control Structures usage

10 marks

Outputs

Format

2.5 marks

Correctness

2.5 marks

Variables usage

  1. marks
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C code::


#include <stdio.h>
#include <stdbool.h>

//true if year is leap
bool isLeap(int y)
{
return (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0));
}

// check date is valid
int validatedate(int d, int m, int y)
{
if((y < 9999) && (y >= 1800)) // change year limit
{
if((m >= 1) && (m <= 12)) // check month
{
if((d >= 1) && (d <= 31))
{
if(m == 2)
{
if(isLeap(y))
{
if(d > 29)
{
printf("Invalid day of feb");
return -1;
}
}
else
{
if(d > 28)
{
printf("Invalid day of feb");
return -1;
}
}
}

if(m == 4 || m == 6 ||
m == 9 || m == 11)
{
if(d > 30)
{
printf("Invalid day of 30days month");
return -1;
}
}
printf("Date is valid %d-%d-%d",d,m,y);
  
}
else
{
printf("Invalid day");
return -1;
}
}
else
{
printf("Invalid month");
return -1;
}
}
else
{
printf("Invalid year");
return -1;
}
return 0;
}

int dater(int m)
{
int y=0;
switch(m)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120; break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Invalid Input");
return 1;
}
return(y);
}

int main(void) {

int d1,m1,y1;
int d2,m2,y2;
int dd,dd2,mm,yy;
int i;

printf("First a date(dd-mm-yyyy) : ");
scanf("%d-%d-%d",&d1,&m1,&y1);

printf("Second a date(dd-mm-yyyy) : ");
scanf("%d-%d-%d",&d2,&m2,&y2);

printf(" ");

if((validatedate(d1,m1,y1) == 0) && (validatedate(d2,m2,y2)) == 0)
{

if(y1 < y2)
yy = y1;
else
yy = y2;

dd = dater(m1);

for(i = yy ; i < y1 ; i++)
{
if(i % 4 == 0)
dd+=1;
}

dd=dd+d1+(y1-yy)*365;

dd2=0;

for(i = yy ; i < y2 ; i++)
{
if(i % 4 == 0)
dd2 += 1;
}

dd2=dater(m2)+dd2+d2+((y2-yy)*365);
i = 0;
if(dd2 > dd)
i = dd2 - dd;
else
i = dd - dd2;

printf("Difference between the two dates is %d days",i);

// find bigger date
if(y1 > y2)
printf("Date %d-%d-%d is bigger",d1,m1,y1);
else if (y1 == y2)
{
if(m1 == m2)
{
if(d1 == d2)
{
printf ("Date %d-%d-%d is equal to date %d-%d-%d",d1,m1,y1,d2,m2,y2);
}
else if(d1 > d2)
{
printf("Date %d-%d-%d is bigger",d1,m1,y1);
}
else
{
printf("Date %d-%d-%d is bigger",d2,m2,y2);
}
}
else if (m1 > m2)
{
printf("Date %d-%d-%d is bigger",d1,m1,y1);
}
else
{
printf("Date %d-%d-%d is bigger",d2,m2,y2);
}
}
else
{
printf("Date %d-%d-%d is bigger",d2,m2,y2);
}
}

return 0;
}

Output::

Add a comment
Know the answer?
Add Answer to:
Question 2: WRITE A C PROGRAMME TO Accept dates in ‘dd-mm-yyyy’ format into string variables and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • Show data: Show Name (string- up to 25 characters, may not be blank) Show Date (mm/dd/yyyy,...

    Show data: Show Name (string- up to 25 characters, may not be blank) Show Date (mm/dd/yyyy, in that format) Show Fee to Participate (float, >-0) Show Sales (float, >-0) Miscellaneous Costs (float, >-0) Rate the Show(rating must be one of those 5 choices) Above maino, create a structure that will hold the information listed above. Then inside of main) create an array of 10 structures. Each structure in the array will represent a different show. Rules of the program: Option...

  • need this in #c . You will now add server side validation code to the frmPersonnel...

    need this in #c . You will now add server side validation code to the frmPersonnel page. Currently, when the Submit button is pressed, the frmPersonnelVerified page is displayed. This is because the frmPersonnelVerified page is set as the Submit button's PostBackUrl property. Instead of having the page go directly to the frmPersonnelVerified page when the Submit button is pressed, we want to do some server side validation. If any of the validation rules fail, we will redisplay the frmPersonnel...

  • pls help me with it. you just need to answer the question in Appointment.Java, There is...

    pls help me with it. you just need to answer the question in Appointment.Java, There is only 1 question u need to answer! Appointment.Java package option1.stage3; import option1.stage1.Doctor; import option1.stage1.Patient; import option1.stage2.TimeSlot; public class Appointment { private Doctor doctor; private Patient patient; private TimeSlot timeSlot; public Doctor getDoctor() { return doctor; } public void setDoctor(Doctor doctor) { this.doctor = doctor; } public Patient getPatient() { return patient; } public void setPatient(Patient patient) { this.patient = patient; } public TimeSlot getTimeSlot()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT