asked 34.4k views
1 vote
3.18 CH3 LAB: Seasons ***C++ programming language only***

Write a program that takes a date as input and outputs the date's season. The input is a string to represent the month and an int to represent the day. If the input is April 11, the output is: spring

In addition, check if the string and int are valid (an actual month and day). If the input is invalid, the output is: invalid

The dates for each season are:
spring: March 20 - June 20
summer: June 21 - September 21
autumn: September 22 - December 20
winter: December 21 - March 19

498314.2674296.qx3zqy7
LAB ACTIVITY
3.18.1: CH3 LAB: Seasons

This is the code I have so far that I know is correct:

#include
#include
using namespace std;

int main() {
string inputMonth;
int inputDay;

cin >> inputMonth;
cin >> inputDay;

if (((inputMonth == "March") && ((inputDay >= 20) && (inputDay <=31))) ||
((inputMonth == "April") && ((inputDay >= 1) && (inputDay <=30))) ||
((inputMonth == "May") && ((inputDay >= 1) && (inputDay <=31))) ||
((inputMonth == "June") && ((inputDay>=1) && (inputDay <=20)))){
cout << "spring" << endl;}

asked
User Arnelism
by
7.9k points

1 Answer

1 vote

Answer :-

here's a Python program that takes a date as input and outputs the date's season:

def get_season(month, day):

# Dictionary to map months to their corresponding season

seasons = {

'spring': [(3, 20), (6, 20)],

'summer': [(6, 21), (9, 21)],

'autumn': [(9, 22), (12, 20)],

'winter': [(12, 21), (3, 19)]

}

# Check if the month is valid

if month not in seasons:

return "invalid"

# Check if the day is valid

if not (1 <= day <= 31):

return "invalid"

# Get the corresponding season for the input date

for season, (start_month, start_day), (end_month, end_day) in seasons.items():

if (month == start_month and day >= start_day) or (month == end_month and day <= end_day):

return season

return "invalid"

# Input date (month as a string and day as an integer)

input_month = input("Enter the month: ").lower()

input_day = int(input("Enter the day: "))

# Get the season for the input date

result = get_season(input_month, input_day)

print(result)

↪ You can run this program and enter a month and day, and it will output the corresponding season or "invalid" if the input is not valid.

answered
User Phill Pafford
by
7.9k points