w3resource

C Exercises: Read month number and display month name

C Conditional Statement: Exercise-23 with Solution

Write a C program for reading any Month Number and displaying the Month name as a word.

Visual Presentation:

Read month number and display month name

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.

void main()
{
   int monno;  // Declare a variable to store the input month number.

   printf("Input Month No : ");  // Prompt the user to input a month number.
   scanf("%d",&monno);  // Read and store the input month number.

   switch(monno)  // Start a switch statement based on the input month number.
   {
	case 1:
	       printf("January\n");  // Print "January" for input month number 1.
	       break;
	case 2:
	       printf("February\n");  // Print "February" for input month number 2.
	       break;
	case 3:
	       printf("March\n");  // Print "March" for input month number 3.
	       break;
	case 4:
	       printf("April\n");  // Print "April" for input month number 4.
	       break;
	case 5:
	       printf("May\n");  // Print "May" for input month number 5.
	       break;
	case 6:
	       printf("June\n");  // Print "June" for input month number 6.
	       break;
	case 7:
	       printf("July\n");  // Print "July" for input month number 7.
	       break;
	case 8:
	       printf("August\n");  // Print "August" for input month number 8.
	       break;
	case 9:
	       printf("September\n");  // Print "September" for input month number 9.
	       break;
	case 10:
	       printf("October\n");  // Print "October" for input month number 10.
	       break;
	case 11:
	       printf("November\n");  // Print "November" for input month number 11.
	       break;
	case 12:
	       printf("December\n");  // Print "December" for input month number 12.
	       break;
	default:
	       printf("Invalid Month number. \nPlease try again ....\n");  // Print a message for an invalid input.
	       break;
      }
}

Sample Output:

Input Month No : 4                                                                                            
April  

Flowchart:

Flowchart: Read month number and display month name

C Programming Code Editor:

Previous: Write a program in C to read any digit, display in the word.
Next: Write a program in C to read any Month Number in integer and display the number of days for this month.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.