You're online now.

Hurray! you are online now.

Display Name of the month by accepting digit of the month | c programming

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”.

// Using switch case
    #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;
    }
// Using if-else statement
    #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;
    }

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 match, if the case 4 is matched then it will print “April”. Similarly, it the if else statement, we will also check OK.

🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...