Display Name of the Month by Accepting Number of the Month in C/C++

Introduction

In this post, I am going to write a program in c and c++ to display name of the month by accepting number of the month.

Also Read: Tokens in C++ Programming

This is a very simple program. We can write this program using if-else statement or switch case statement. I would prefer to write this program using switch case.

In this program, we are asking the user to enter a number between 1 to 12. If he enters 3, then output will be “March”. If he enters 11, then we will display the month “November” and so on.

Also Read: Best 5 Basic C++ Programs For Beginners

See the following expected output.

Output

display name of the month by accepting number of the month

I hope you have understood what we have to do. This output will remain the same for both c and c++ programs.

Display Name of the Month by Accepting Number of the Month in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int number;
    printf("Enter a month number\n");
    scanf("%d",&number);
    switch(number)
    {
    case 1:
        printf("January");
        break;
    case 2:
        printf("February");
        break;
    case 3:
        printf("March");
        break;
    case 4:
        printf("April");
        break;
    case 5:
        printf("May");
        break;
    case 6:
        printf("June");
        break;
    case 7:
        printf("July");
        break;
    case 8:
        printf("August");
        break;
    case 9:
        printf("September");
        break;
    case 10:
        printf("October");
        break;
    case 11:
        printf("November");
        break;
    case 12:
        printf("December");
        break;
    default:
        printf("Invalid month number");
    }
    return 0;
}

Display Name of the Month by Accepting Number of the Month in C++

#include <iostream>
using namespace std;
int main()
{
    int number;
    cout<<"Enter a month number"<<endl;
    cin>>number;
    switch(number)
    {
    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 month number";
    }
    return 0;
}

In the both programs, I have used switch case statements. There are 12 cases for each month.

Also Read: Simple Interest Program in C++ using Class

I hope you have understood both these programs. If you have any difficulty, feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment