Open In App
Related Articles

Print the season name of the year based on the month number

Improve Article
Improve
Save Article
Save
Like Article
Like

Given the month number M, the task is to print the season name of the year based on the month number.
Examples: 

Input: M = 5
Output: SPRING
Input: M = 1
Output: WINTER

Approach: 

  • There are 4 main seasons in a year, that is, Summer, Autumn, Winter and Spring.
  • The winter months are in December, January and February.
  • The spring months in March, April and May.
  • The summer months in June, July and August.
  • And the autumn months in September, October and November.
  • So map the month to the particular season respectively and print it.

Below is the implementation of the above approach: 

C++




// C++ program to print the season
// name based on the month number
#include <bits/stdc++.h>
using namespace std;
 
void findSeason(int M)
{
 
    // Checks out the season according
    // to the month number entered by the user
    switch (M)
    {
        case 12:
        case 1:
        case 2:
            cout << ("\nWINTER");
            break;
        case 3:
        case 4:
        case 5:
            cout << ("\nSPRING");
            break;
        case 6:
        case 7:
        case 8:
            cout << ("\nSUMMER");
            break;
        case 9:
        case 10:
        case 11:
            cout << ("\nAUTUMN");
            break;
        default:
         
            // Handles the condition if number entered
            // is not among the valid 12 months
            cout << ("\nInvalid Month number");
            break;
    }
}
 
// Driver code
int main()
{
    int M = 5;
    cout << "For Month number: " << M;
    findSeason(M);
 
    M = 10;
    cout << "\nFor Month number: " << M;
    findSeason(M);
    return 0;
}
 
// This code is contributed by Rajput-Ji


Java




// Java program to print the season
// name based on the month number
 
import java.util.*;
public class Seasons {
 
    public static void findSeason(int M)
    {
 
        // Checks out the season according
        // to the month number entered by the user
        switch (M) {
        case 12:
        case 1:
        case 2:
            System.out.println("WINTER");
            break;
        case 3:
        case 4:
        case 5:
            System.out.println("SPRING");
            break;
        case 6:
        case 7:
        case 8:
            System.out.println("SUMMER");
            break;
        case 9:
        case 10:
        case 11:
            System.out.println("AUTUMN");
            break;
        default:
            // Handles the condition if number entered
            // is not among the valid 12 months
            System.out.println("Invalid Month number");
            break;
        }
    }
 
    // Driver Code
    public static void main(String abc[])
    {
        int M = 5;
        System.out.println("For Month number: "
                           + M);
        findSeason(M);
 
        M = 10;
        System.out.println("For Month number: "
                           + M);
        findSeason(M);
    }
}


Python3




# Python3 program to print the season
# name based on the month number
def findseason (M) :
     
    # Taken all the possible
    # month numbers in the list.
    list1 = [[12 , 1 , 2], [3 , 4 , 5],
             [6 , 7 , 8], [9 , 10 , 11]]
              
    # Matching the month number
    # with the above list entries
    if M in list1[0] :
        print ( "WINTER" )
    elif M in list1[1] :
        print ( "SPRING" )
    elif M in list1[2] :
        print ( "SUMMER" )
    elif M in list1[3] :
        print ( "AUTUMN" )
    else :
        print ( "Invalid Month Number" )
 
# Driver Code
M = 5
print("For Month number:", M);
findseason ( M )
 
M = 10
print("For Month number:", M);
findseason ( M )
 
# This code is contributed by Abhishek


C#




// C# program to print the season
// name based on the month number
using System;
 
class GFG
{
public static void findSeason(int M)
{
 
    // Checks out the season according
    // to the month number entered by the user
    switch (M)
    {
        case 12:
        case 1:
        case 2:
            Console.WriteLine("WINTER");
            break;
        case 3:
        case 4:
        case 5:
            Console.WriteLine("SPRING");
            break;
        case 6:
        case 7:
        case 8:
            Console.WriteLine("SUMMER");
            break;
        case 9:
        case 10:
        case 11:
            Console.WriteLine("AUTUMN");
            break;
        default:
            // Handles the condition if number entered
            // is not among the valid 12 months
            Console.WriteLine("Invalid Month number");
            break;
    }
}
 
// Driver Code
public static void Main()
{
    int M = 5;
    Console.WriteLine("For Month number: " + M);
    findSeason(M);
 
    M = 10;
    Console.WriteLine("For Month number: " + M);
    findSeason(M);
}
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
    // Javascript program to print the season
// name based on the month number
 
    function findSeason(M)
    {
   
        // Checks out the season according
        // to the month number entered by the user
        switch (M) {
        case 12:
        case 1:
        case 2:
            document.write("WLETER" + "<br/>");
            break;
        case 3:
        case 4:
        case 5:
            document.write("SPRING" + "<br/>");
            break;
        case 6:
        case 7:
        case 8:
            document.write("SUMMER"  + "<br/>");
            break;
        case 9:
        case 10:
        case 11:
            document.write("AUTUMN" + "<br/>");
            break;
        default:
            // Handles the condition if number entered
            // is not among the valid 12 months
            document.write("Invalid Month number");
            break;
        }
    }
 
     
    // Driver code
     
        let M = 5;
        document.write("For Month number: "
                           + M + "<br/>");
        findSeason(M);
   
        M = 10;
        document.write("For Month number: "
                           + M + "<br/>");
        findSeason(M);
  
 // This code is contributed by susmitakundugoaldanga.
</script>


Output

For Month number: 5
SPRING
For Month number: 10
AUTUMN

Time Complexity: O(1)
Auxiliary Space: O(1)

Dictionary Mapping

We can create a dictionary to map the month number to its corresponding season. Then we can simply retrieve the value from the dictionary using the input month number.

Steps:

  • Create a dictionary with the keys as month numbers and the values as corresponding seasons.
  • Take the input month number from the user.
  • Retrieve the value from the dictionary using the input month number.
  • Print the season name.
     

C++




#include <iostream>
#include <unordered_map>
#include <string>
 
int main()
{
   
    // Create an unordered map that maps month numbers to season names.
    std::unordered_map<int, std::string> seasons {
        {1, "WINTER"},
        {2, "WINTER"},
        {3, "SPRING"},
        {4, "SPRING"},
        {5, "SPRING"},
        {6, "SUMMER"},
        {7, "SUMMER"},
        {8, "SUMMER"},
        {9, "FALL"},
        {10, "FALL"},
        {11, "FALL"},
        {12, "WINTER"}
    };
 
    // Set the month number to check.
    int M = 1;
 
    // Get the season name for the given month number.
    std::string season = seasons[M];
 
    // Print the season name to the console.
    std::cout << season << std::endl;
 
    return 0;
}


Java




import java.util.HashMap;
 
public class GFG {
    public static void main(String[] args) {
 
        // Create a HashMap that maps month numbers to season names.
        HashMap<Integer, String> seasons = new HashMap<>();
        seasons.put(1, "WINTER");
        seasons.put(2, "WINTER");
        seasons.put(3, "SPRING");
        seasons.put(4, "SPRING");
        seasons.put(5, "SPRING");
        seasons.put(6, "SUMMER");
        seasons.put(7, "SUMMER");
        seasons.put(8, "SUMMER");
        seasons.put(9, "FALL");
        seasons.put(10, "FALL");
        seasons.put(11, "FALL");
        seasons.put(12, "WINTER");
 
        // Set the month number to check.
        int M = 1;
 
        // Get the season name for the given month number.
        String season = seasons.get(M);
 
        // Print the season name to the console.
        System.out.println(season);
    }
}


Python3




# Step 1
seasons = {
    1: 'WINTER',
    2: 'WINTER',
    3: 'SPRING',
    4: 'SPRING',
    5: 'SPRING',
    6: 'SUMMER',
    7: 'SUMMER',
    8: 'SUMMER',
    9: 'FALL',
    10: 'FALL',
    11: 'FALL',
    12: 'WINTER'
}
 
# Step 2
M = 1
 
# Step 3
season = seasons.get(M)
 
# Step 4
print(season)


C#




using System;
using System.Collections.Generic;
 
class Program {
    static void Main() {
        // Create a dictionary that maps month numbers to season names.
        Dictionary<int, string> seasons = new Dictionary<int, string>() {
            {1, "WINTER"},
            {2, "WINTER"},
            {3, "SPRING"},
            {4, "SPRING"},
            {5, "SPRING"},
            {6, "SUMMER"},
            {7, "SUMMER"},
            {8, "SUMMER"},
            {9, "FALL"},
            {10, "FALL"},
            {11, "FALL"},
            {12, "WINTER"}
        };
 
        // Set the month number to check.
        int M = 1;
 
        // Get the season name for the given month number.
        string season = seasons[M];
 
        // Print the season name to the console.
        Console.WriteLine(season);
    }
}


Javascript




// Step 1
const seasons = {
1: 'WINTER',
2: 'WINTER',
3: 'SPRING',
4: 'SPRING',
5: 'SPRING',
6: 'SUMMER',
7: 'SUMMER',
8: 'SUMMER',
9: 'FALL',
10: 'FALL',
11: 'FALL',
12: 'WINTER'
};
 
// Step 2
const M = 1;
 
// Step 3
const season = seasons[M];
 
// Step 4
console.log(season);


Output

WINTER

Time Complexity: O(1)
Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 02 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials