You must Sign In to post a response.
  • Category: Programming

    C program to check whether entered character is vowel or consonant using switch statement


    Confused with answering an exam question of an MSBTE computer engineering paper? From the answers at this Ask Expert page, you will know the solution.

    Following is a question that was asked in the MSBTE (Maharashtra State Board of Technical Education, Mumbai)for Diploma in Computer Engineering question paper of C Programming Language (22226).

    This question is asked for 6 marks and hence requires some explanation along with the program code. Please let me know the solution -
    Q) Write a C Program using switch statement to check whether entered character is vowel or consonant.
  • Answers

    1 Answers found.
  • In C language, switch statement is used as decision making statement. When there is a situation to select only one option from available multiple options, then switch statement is used. So while constructing menu driven program switch is the best option. Switch statement have multiple case values and user given value is tested with available case values. If match is found between case value and user defined value then corresponding case statement get executed. Otherwise next case is taken for matching and so on. If no case value have match with user defined value then default statement get executed. Every case value have break statement so that after executing particular case the control will exit the switch with executing next case.
    If multiple case statements are used without using break statement in between case values then case statements get followed still break statement does not appear. This concept is used to solve this program.

    Below is the program to find whether given character is vowel or not using switch statement.
    Please include stdio.h and conio.h header files before main function.
    //PROGRAM TO CHECK WHETHER GIVEN CHARACTER IS VOWEL OR NOT USING SWITCH
    #include
    #include
    void main(void)
    {
    char var;
    clrscr();
    printf("Enter any character to check whether it is vowel or not\n");
    scanf("%c",&var);
    switch(var)
    {
    case 'a':
    case 'A':
    printf("%c is a vowel character",var);
    break;
    case 'e':
    case 'E':
    printf("%c is a vowel character",var);
    break;
    case 'i':
    case 'I':
    printf("%c is a vowel character",var);
    break;
    case 'o':
    case 'O':
    printf("%c is a vowel character",var);
    break;
    case 'u':
    case 'U':
    printf("%c is a vowel character",var);
    break;
    default:
    printf("%c is not a vowel character",var);
    }
    getch();
    }
    In above program if we enter A as input for var variable then it first check with value 'a'. Actually they does not match because var contains A and case value is a. But there is no any break statement between case 'a' and case 'A'. So control come to case 'A'. Here match is found and so it prints that given character is vowel, now break appears and hence control will come out of switch statement.
    If entered value is e, then it first check case 'a' which does not match, then case 'A' which also does not match. Here both cases does not have match so it comes to case 'e' and match is found. Still it goes to case 'E' which does not have match. Case 'e' have match but case 'E' does not have match and break statement is absent between these case statements. As one case is true and another is false it follow the printf() statement and then exit from the switch as break statement appears.
    The same procedure is followed if entered character is vowel. But if entered character is not a vowel then it does not found match with any case value. So it comes to default statement. Which prints that given character is not a vowel character.


  • Sign In to post your comments