18.Create a program in C to determine the season based on the month using if else statements.
Purpose:
The purpose of the program is to determine the season based on the input month provided by the user. By using if else statements to compare the month input, the program can accurately identify the corresponding season e.g. winter, spring, summer or autumn. This can be helpful in various applications where seasonal information is required, such as in weather forecasting, agricultural planning, and event scheduling.
Agenda:
1. Define a variable to store the input month provided by the user.
2. Prompt the user to input the month e.g. 1 for January, 2 for February, etc.
3. Use if else statements to compare the input month and determine the corresponding season based on the month ranges.
4. Display the season corresponding to the input month to the user as the output.
Real-life applications:
1. Weather Forecasting: Meteorological applications can use this program to categorize weather conditions based on the current month and predict seasonal patterns.
2. Agricultural Planning: Farmers can utilize this program to determine the appropriate crops to plant or harvest based on the season indicated by the month.
3. Event Scheduling: Event organizers can schedule outdoor activities or festivals based on the season determined by the program to ensure optimal conditions for participants.
A program in C to determine the season based on the month using if else statements.
/* C language program to determine the season based on the month using if else statements.*/
#include <stdio.h>
int main()
{
int month;
// Input month from user
printf("Enter a month (1-12): ");
scanf("%d", &month);
// Determine season based on month
if (month >= 1 && month <= 12)
{
if (month == 12 || month <= 2)
{
printf("Winter\n");
}
else if (month >= 3 && month <= 5)
{
printf("Spring\n");
} else if (month >= 6 && month <= 8)
{
printf("Summer\n");
} else {
printf("Autumn\n");
}
} else
{
printf("\n Please enter a number between 1 and 12. Invalid month entered. \n");
}
return 0;
}
Output
Enter a month (1-12)
4
Spring