Question:
Reaya’s teacher has asked her to prepare well for the lesson on seasons. When her teacher tells a month, she needs to say the season corresponding to that month. Write a program to solve the above task.
- Spring – March to May,
- Summer – June to August,
- Autumn – September to November and,
- Winter – December to February.
Month should be in the range 1 to 12. If not the output should be “Invalid month”.
Sample Input 1:
Enter the month:11
Sample Output 1:
Season:Autumn
Sample Input 2:
Enter the month:13
Sample Output 2:
Invalid month
Code:
import java.util.Scanner;
public class Season
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the month:");
int mon=sc.nextInt();
if(mon>12||mon<1)
{
System.out.println("Invalid month");
}
else if(mon>=3&&mon<=5)
{
System.out.println("Season:Spring");
}
else if(mon>=6&&mon<=8)
{
System.out.println("Season:Summer");
}
else if(mon>=9&&mon<=11)
{
System.out.println("Season:Autumn");
}
else if(mon==12||mon==1||mon==2)
{
System.out.println("Season:Winter");
}
}
}
ConversionConversion EmoticonEmoticon