Check vowel or consonant using switch case in C Programming

  • Tech Area
  • May 11, 2024



In this tutorial, We will see how to check vowel or consonant using switch case in C programming language. The switch expression is evaluated once. Switch statement allows us to execute one code block among many alternatives.

The syntax of the switch case is:

switch (expression)
​{
    case 1:
      // statements
      break;

    case 2:
      // statements
      break;
    .
    .
    default:
      // default statements
}

How does the switch statement work?

The expression is evaluated once and compared with the values of each case label i.e. case 1, case 2 and so on.

If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to case 2, statements after case 2: are executed until break is encountered.

If there is no match, the default statements are executed.

Program to check vowel and consonant using Switch statement

#include<stdio.h>
#include<conio.h>
void main()
{
char z;
clrscr();
printf("Enter any character\n");
scanf("%c", &z);
switch(z)
{
case 'A':
case 'a':
printf("%c is a vowel", z);
break;

case 'E':
case 'e':
printf("%c is a vowel", z);
break;

case 'I':
case 'i':
printf("%c is a vowel", z);
break;

case 'O':
case 'o':
printf("%c is a vowel", z);
break;

case 'U':
case 'u':
printf("%c is a vowel", z);
break;

default:
printf("%c is a consonant", z);
}
getch();
}

Output

Enter any character
a
a is a vowel

The character a entered by the user is stored in the z variable. Since the z is a, the control of the program jumps to case 'a'.

case 'A':
case 'a':
printf("%c is a vowel", z);
break;

Finally, the break statement terminates the switch statement.


Subscribe us via Email

Join 10,000+ subscriber

Subscribe on YouTube