How to find month name corresponding to number using switch case in C++ Language

Introduction: In previous articles i explained the program  for How to find week day corresponding to number using switch case in C++ Language and  Else if ladder or multiple else if to check the day corresponding to number and How to find greatest of three numbers using multiple if statement and What is For loop in C++ Language? Example program to print counting up to given limit and What is goto statement,its advantages and disadvantages with program example  and Get number of days in month,year using nested if and Program to check for vowel or consonant using switch case and Swap two numbers without using temporary variable and Check whether a number is positive or negative using if else and Check whether a number is even or odd using if else and  many other C++ programs.
In this post i have written a program to check the month name corresponding to entered number using switch case statement in C++ Language.

Description: The switch statement is a control statement that handles multiple selections by passing control to one of the case statements within its body.

It accepts single input from the user and based on that input executes a particular block of statements. Control passes to the statement whose case constant-expression matches the value of switch (expression).

Implementation: Let's create a program to understand the concept.

#include<conio.h>
#include<iostream.h>
main()
{
      int num;
      clrscr();
      cout<<"Enter any number between 1 to 12: ";
      cin>>num;
      switch(num)
      {
      case 1:
                   cout<<"January";
                   break; 
      case 2:
                   cout<<"February";
                   break; 
      case 3:
                   cout<<"March";
                   break; 
      case 4:
                   cout<<"April";
                   break; 
      case 5:
                   cout<<"May";
                   break; 
      case 6:
                   cout<<"June";
                   break; 
      case 7:
                   cout<<"July";
                   break; 
     case 8:
                   cout<<"August";
                   break; 
      case 9:
                   cout<<"September";
                   break; 
      case 10:
                   cout<<"October";
                   break; 
      case 11:
                   cout<<"November";
                   break; 
      case 12:
                   cout<<"December";
                   break; 
      default:
                   cout<<"INVALID NUMBER!!! Enter number between 1 to 12";
      } 
       getch(); 
      }

  • Run the program using Ctrl+F9
Output
First Run:
 Enter any number between 1 to 12 : 10
 October

Second Run:
 Enter any number between 1 to 12 : 15

 INVALID NUMBER!!! Enter number between 1 to 12

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..