Programs to find the name of a month according to the input number are shown here. For example if the input number is 1, the month is January and if the input number is 12, the month is December. Each month according to the input number has been displayed in the following table.
Input Number | Month |
---|---|
1 | January |
2 | February |
3 | March |
4 | April |
5 | May |
6 | June |
7 | July |
8 | August |
9 | September |
10 | October |
11 | November |
12 | December |
1. Programs to find the name of a month according to the input number
In this section, the C, C++, Java, Python and C# programs to find the name of a month according to the input number are given. A valid input number ranges from 1 to 12.
1.1. C Programs to find the name of a month according to the input number
/*********************************** alphabetacoder.com C program to display name of the month according to the input number ************************************/ #include <stdio.h> int main() { // declare variables int num; // take input printf("Enter a number between 1 to 12 inclusive: "); scanf("%d", & num); // display the month switch (num) { case 1: printf("The month is January."); break; case 2: printf("The month is February."); break; case 3: printf("The month is March."); break; case 4: printf("The month is April."); break; case 5: printf("The month is May."); break; case 6: printf("The month is June."); break; case 7: printf("The month is July."); break; case 8: printf("The month is August."); break; case 9: printf("The month is September."); break; case 10: printf("The month is October."); break; case 11: printf("The month is November."); break; case 12: printf("The month is December."); break; default: printf("Wrong input!"); } return 0; }
Output
Case 1:
Enter a number between 1 to 12 inclusive: 1
The month is January.
Case 2:
Enter a number between 1 to 12 inclusive: 2
The month is February.
Case 3:
Enter a number between 1 to 12 inclusive: 0
Wrong input!
1.2. C++ Programs to find the name of a month according to the input number
/*********************************** alphabetacoder.com C++ program to display name of the month according to the input number ************************************/ #include <iostream> using namespace std; int main() { // declare variables int num; // take input cout << "Enter a number between 1 to 12 inclusive: "; cin >> num; // display the month switch (num) { case 1: cout << "The month is January."; break; case 2: cout << "The month is February."; break; case 3: cout << "The month is March."; break; case 4: cout << "The month is April."; break; case 5: cout << "The month is May."; break; case 6: cout << "The month is June."; break; case 7: cout << "The month is July."; break; case 8: cout << "The month is August."; break; case 9: cout << "The month is September."; break; case 10: cout << "The month is October."; break; case 11: cout << "The month is November."; break; case 12: cout << "The month is December."; break; default: cout << "Wrong input!"; } return 0; }
Output
Case 1:
Enter a number between 1 to 12 inclusive: 2
The month is February.
Case 2:
Enter a number between 1 to 12 inclusive: 11
The month is November.
Case 3:
Enter a number between 1 to 12 inclusive: 3
The month is March.
1.3. Java Programs to find the name of a month according to the input number
/*********************************** alphabetacoder.com Java program to display name of the month according to the input number ************************************/ import java.util.Scanner; class Main { public static void main(String[] args) { // declare instance of Scanner class Scanner sc = new Scanner(System.in); // declare variables int num; // take input System.out.print("Enter a number between 1 to 12 inclusive: "); num = sc.nextInt(); // display the month switch (num) { case 1: System.out.print("The month is January."); break; case 2: System.out.print("The month is February."); break; case 3: System.out.print("The month is March."); break; case 4: System.out.print("The month is April."); break; case 5: System.out.print("The month is May."); break; case 6: System.out.print("The month is June."); break; case 7: System.out.print("The month is July."); break; case 8: System.out.print("The month is August."); break; case 9: System.out.print("The month is September."); break; case 10: System.out.print("The month is October."); break; case 11: System.out.print("The month is November."); break; case 12: System.out.print("The month is December."); break; default: System.out.print("Wrong input!"); } } }
Output
Case 1:
Enter a number between 1 to 12 inclusive: 4
The month is April.
Case 2:
Enter a number between 1 to 12 inclusive: 10
The month is October.
Case 3:
Enter a number between 1 to 12 inclusive: 13
Wrong input!
1.4. Python Programs to find the name of a month according to the input number
#************************************** # alphabetacoder.com #Python program to display name of the #month according to the input number #************************************** # take input num = int(input("Enter a number between 1 to 12 inclusive: ")) # display the month if num == 1: print("The month is January.") elif num == 2: print("The month is February.") elif num == 3: print("The month is March.") elif num == 4: print("The month is April.") elif num == 5: print("The month is May.") elif num == 6: print("The month is June.") elif num == 7: print("The month is July.") elif num == 8: print("The month is August.") elif num == 9: print("The month is September.") elif num == 10: print("The month is October.") elif num == 11: print("The month is November.") elif num == 12: print("The month is December.") else: print("Wrong input!")
Output
Case 1:
Enter a number between 1 to 12 inclusive: 5
The month is May.
Case 2:
Enter a number between 1 to 12 inclusive: 9
The month is September.
Case 3:
Enter a number between 1 to 12 inclusive: -2
Wrong input!
1.5. C# Programs to find the name of a month according to the input number
/*********************************** alphabetacoder.com C# program to display name of the month according to the input number ************************************/ using System; namespace Month { class Program { static void Main(string[] args) { // declare variables int num; // take input Console.Write("Enter the num: "); num = Convert.ToInt32(Console.ReadLine()); // display the month switch (num) { case 1: Console.WriteLine("The month is January."); break; case 2: Console.WriteLine("The month is February."); break; case 3: Console.WriteLine("The month is March."); break; case 4: Console.WriteLine("The month is April."); break; case 5: Console.WriteLine("The month is May."); break; case 6: Console.WriteLine("The month is June."); break; case 7: Console.WriteLine("The month is July."); break; case 8: Console.WriteLine("The month is August."); break; case 9: Console.WriteLine("The month is September."); break; case 10: Console.WriteLine("The month is October."); break; case 11: Console.WriteLine("The month is November."); break; case 12: Console.WriteLine("The month is December."); break; default: Console.WriteLine("Wrong input!"); break; } // wait for user to press any key Console.ReadKey(); } } }
Output
Case 1:
Enter a number between 1 to 12 inclusive: 6
The month is June.
Case 2:
Enter a number between 1 to 12 inclusive: 8
The month is August.
Case 3:
Enter a number between 1 to 12 inclusive: 7
The month is July.