Description

We will print the month name from the month number given by the user which will give us the user input.

For Example : User Entered 5 then print "May".

Now Let's start the coding...

#include<stdio.h>

int main(){
    int num;
    printf("Enter the number of Month : ");
    scanf("%d", &num);

    switch(num){
        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("Please Enter Valid Number of the Month!");
      }
      return 0;
}
#include<stdio.h>

int main(){

    int num;
    printf("Enter the number of Month : ");
    scanf("%d", &num);

    if(num == 1){
        printf("January");
    }else if(num == 2){
        prinf("February");
    }else if(num == 3){
        printf("March");
    }else if(num == 4){
        printf("April");
    }else if(num == 5){
        printf("May");
    }else if(num == 6){
        printf("June");
    }else if(num == 7){
        printf("July");
    }else if(num == 8){
        printf("August");
    }else if(num == 9){
        printf("September");
    }else if(num == 10){
        printf("Ocotber");
    }else if(num == 11){
        printf("November");
    }else if(num == 12){
        printf("December");
    }else{
        printf("Please Enter Valid number of Month!");
    }
    
    return 0;
}

Ouput : 

Enter the number of Month : 6
June

How it's work?

We have written the same program in two ways, one by using switch statement and the other by using if statement. Same output will come from both the programs.
So let's see how it is working.
In both the programs, we first created a variable 'num' to store the input given by the user and then through the switch statement, we will match it through the case, whatever will be matched, whatever its statement will be printed. Suppose the user has given input 4 then it will match the case as soon as the case is matched, if the case 4 is matched then it will print "April".
Similarly, in the if else statement, we will also check OK.

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Happy Numbers | with Example And Programs

Replace the number by the sum of the squares of its digits, and repeat the process. At the end, if the number is equals to 1 then it is a Happy Number, or it loops endlessly in a cycle that does not include 1 (if it is not a happy number then this process will end at 4).

Sunny Number in programming

A number is a sunny number. If 1 is added to that number and the square root of it becomes a whole number.

Program to check whether number is Disarium Number

A Number is a disarium number if the sum of the digits powered with their respective positions is equal to the number itself.

Floyd Triangle | Programs | Algorithm

A Floyd Triangle is right angled triangle consisting of consecutive natural numbers starting with one and then going down through the rows.

Write a program to reverse a number

Write a program to reverse a number. What is reverse number?

Sum of digit calculate program

We can get the sum of the digit by adding every digits of the given number forgetting the place of value of the digit.