Question

   Lab/HW 3   Write a program that reads in the following data, all entered on one...

   Lab/HW 3  

Write a program that reads in the following data, all entered on one line with at least one space separating them:

a) an integer representing a month
b) an integer representing the day of the month
c) an integer representing a year

1. Check that the month is between 1 and 12. If it’s not print an error message.

2. Day cannot be 0 or less nor can it be more than 31 for all months. Print an error message if these conditions occur. If day is in the range of 0 and 31, you will still need to check other conditions as noted below in 4.

3. Year cannot be less than 1. Print an error message if that occurs.

If any one of these three conditions occurs, only one error message should print and no further code should be executed. If everything is good so far, then continue as below:

4. Check that day is correct for the month. Remember the poem

“30 days has September, April, June and November,

All the rest have 31 except February which has 28 but in a leap year has 29”

a. For all months, except February, you can check if the date is valid by the poem above.
i. If the month has more days than allowed, print a message saying month number NN can not have XXX days (e.g., if the input is 6 31 2018 the message should say month 6 cannot have 31 days)
b. If the month is February, you’ll need to check if the year is a leap year and then decide if the date is correct.

Page 97 in the textbook shows code to determine a leap year. A year is a leap year if (1) it’s divisible by 400 OR

(2) the year is divisible by 4 AND not divisible by 100.

So, if it’s a leap year any day up to and including 29 is valid, otherwise 28 is the largest date in February. If February has too many days, e.g., the input is 2 29 2001, print an error message stating

2001 is not a leap year, Feb can have only 28 days

5. At any point that you determine that a date is valid, print a message in the format

5 18 2017 is a valid date

6. Your code should print only one message if a date is valid.
7. If it’s invalid, print only one error message. It a date has more than one error for example, the input is 6 45 -1234, it doesn’t matter which error you pick first as long as you indicate only one error.

THESE TEST CASES MUST BE INCLUDED IN YOUR OUTPUT:

Enter month, day, year separated by spaces 6 30 2017

6 30 2017 is a valid date

Enter month, day, year separated by spaces 6 31 2017

month 6 can not have more than 30 days

Enter month, day, year separated by spaces -3 12 2019

-3 is not a valid month

Enter month, day, year separated by spaces 2 29 2000

2 29 2000 is a valid date

Enter month, day, year separated by spaces 2 30 2000

month 2 can not have 30 days

Enter month, day, year separated by spaces 2 -12 2019

month 2 can not have -12 days

Enter month, day, year separated by spaces 2 29 2001

2001 is not a leap year, 29 is invalid

Enter month, day, year separated by spaces 6 32 -109

year can not be negative

Enter month, day, year separated by spaces 6 32 2017

month 6 can not have more than 30 days

There are many ways to approach this problem. I recommend coding the input statements and steps 1-3 first and making sure they work properly.

Then move on to step 4.

If the month is NOT February, then there’s no need to check for a leap year. Just check if the month is Sept, April, June or Nov in which case it should have no more than 30. You already checked that no month has more than 31 in step 2 so there’s no need to check again.

If the month is February, check if it’s a leap year in which case 29 is the max; not a leap year 28 is the max. At any point in the code, if a date is invalid print a message to that effect and exit the program. If all the tests have passed, then the date is valid. You should test your code for determining a leap year separately to make sure it works before you include it in the full solution.

Thank you!!!

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

code:

#include<stdio.h>

int leapyear(int year){//leapyear check function
if (year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
return 0;
else if (year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
return 1;
else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020
return 0;
else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
return 1;
}

int main() {
int day,month,year;//initilization
int month1[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
printf("Enter month, day, year separated by spaces ");
scanf("%d %d %d",&month,&day,&year);//taking input
if(month>12 || month<1){//check valid month
printf("%d is not a valid month",month);
}else if(day<1){//check valid day
printf("month %d can not have %d days",month,day);
}else if(year<1){//check valid year
printf("year can not be negative");
}else{
if(month!=2){//month is not 2
if(day<=month1[month-1]){//check for month have correct no of days
printf("%d %d %d is a valid date",month,day,year);
}else{
printf("month %d can not have more than %d days",month,month1[month-1]);
}
}else{
if(day>29){
printf("month %d can not have %d days",month,day);
}else{
if(leapyear(year)==0){ //check for leap year
printf("%d %d %d is a valid date",month,day,year);
}else if(day<=28){//if not leap year and day <=28
printf("%d %d %d is a valid date",month,day,year);
}else{
printf("%d is not a leap year, 29 is invalid",year);
}
}
}
}
return 0;
}

screenshot with output:

Add a comment
Know the answer?
Add Answer to:
   Lab/HW 3   Write a program that reads in the following data, all entered on one...
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
  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Extend this date validation program, so that it checks for valid leap years (in which the...

    Extend this date validation program, so that it checks for valid leap years (in which the month February has 29, instead of 28, days). A leap year is any year that is divisible by 4 but not divisible by 100, unless it is also divisible by 400. Do not allow February to have 29 days on years that are not leap years. **Please view both photos for the entire code and post new code with the leap year modification. Thank...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • CK CUNYOMP 167 al 2019/chapter/ X M X C Does Musical WePACCOU XG Persol chapter/4/section/23 Inbox...

    CK CUNYOMP 167 al 2019/chapter/ X M X C Does Musical WePACCOU XG Persol chapter/4/section/23 Inbox (2,146) - ngomez Olongw 4.23 Homework 4-3 Write a Java program that asks the user for a gate entered as 4 integers dayNumber monthNumber date year. Where dayNumber An integer from 1-7 where 1 Sunday, 2 - Monday.... 7 Saturday monthNumber An integer from 1-12, where 1 - January, 2 - February, 12 - December date An integer from 1-31 representing the date. year...

  • Write a Java console application that reads a string from the keyboard and tests whether it...

    Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is NOT valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). You can use: String monthPart = inputDate.substring(0, 2); int month =...

  • 3. Using no date-specific Java libraries, write a program that computes the number of days between...

    3. Using no date-specific Java libraries, write a program that computes the number of days between any two dates (inclusive). You must take into account leap years. The rule for computing a leap year is as follows: If the year is divisible by 4 it is a Leap Year ... Unless the year is divisible by 100, then it is _not_ a Leap Year ... Unless the year is divisible by 400, then it _is_ a Leap Year. During a...

  • C++ code: Days in current month: Write a program that can determine the number of days...

    C++ code: Days in current month: Write a program that can determine the number of days in a month for a specified month and year. The program should allow a user to enter two integers responding a month and a year, and it should determine how many days are in the specified month. The integers 1 through 12 will be used to identify the months of January through December. The user indicates the end of days in the current month...

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

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
ADVERTISEMENT