Tuesday, October 10, 2017

Print Calendar Month in Words

The program must accept an integer value N and print the corresponding calendar month in words. 1 - January, 2 - February, .... , 11 - November, 12 - December If any value apart from 1 to 12 is passed as input, the program must print "Invalid Input".

Input Format:
The first line denotes the value of N.

Output Format:
The first line contains the output as per the description. (The output is CASE SENSITIVE).

Boundary Conditions:
1 <= N <= 12

Example Input/Output 1:
Input:
5
Output:
May

Example Input/Output 2:
Input:
105
Output:
Invalid Input

Solution 1:
import calendar
n=int(input())
print(calendar.month_name[n]) if n<13 else print('Invalid Input')

Solution 2:
d={1:'January',2:'February',3:'March',4:'April',5:'May',6:'June',7:'July',8:'August',9:'September',10:'October',11:'November',12:'December'}
n=int(input())
print(d[n]) if n<13 else print('Invalid Input')

Solution 3:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
    switch(n)
    {
        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 Input");
    }
}
Share: