Array: Obtain the English name of a given month by its number

Q) Obtain the English name of a given month by its number

Example:

Input:
Enter month number: 4

Output:
April

public String monthName (int n){
  		//create an array containing all the names of the months in order
        String [] months = {"January", "February", "March", "April", "May", "June", "July", "August",
                            "September", "October", "November", "December"};
		//return the (n-1)th month name
        return months[n-1];
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: