Write a program to check whether an alphabet is a vowel or consonant using switch statements only

Last update on August 19 2022 21:50:43 (UTC/GMT +8 hours)

C Conditional Statement: Exercise-17 with Solution

Write a C program to check whether an alphabet is a vowel or consonant.

Pictorial Presentation:

Write a program to check whether an alphabet is a vowel or consonant using switch statements only

Sample Solution:

C Code:

#include <stdio.h>
void main()  
{  
    char sing_ch;  
    
    printf("Input any alphabet : ");  
    scanf("%c", &sing_ch);  
  
    if(sing_ch=='a' || sing_ch=='e' || sing_ch=='i' || sing_ch=='o' || sing_ch=='u' || sing_ch=='A' || sing_ch=='E' || sing_ch=='I' || sing_ch=='O' 

|| sing_ch=='U')  
    {  
        printf("The alphabet is a vowel.\n");  
    }  
    else if((sing_ch>='a' && sing_ch<='z') || (sing_ch>='A' && sing_ch<='Z'))  
    {  
        printf("The alphabet is a consonant.\n");  
    }  
    else  
    {  
        printf("The character is not an alphabet.\n");  
    }   
}
 

Sample Output:

Input any alphabet : K                                                                                        
The alphabet is a consonant. 

Flowchart:

Write a program to check whether an alphabet is a vowel or consonant using switch statements only

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a C program to check whether a character is an alphabet, digit or special character.
Next: Write a C program to calculate profit and loss on a transaction.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

C Programming: Tips of the Day

How to convert an enum type variable to a string?

There really is no beautiful way of doing this. Just set up an array of strings indexed by the enum.

If you do a lot of output, you can define an operator << that takes an enum parameter and does the lookup for you.

Ref: https://bit.ly/2Xm2lMX


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation


WAP in c to check vowel and cosonent using switch

A user inputs a character, and we check whether it’s a vowel or not using switch case statement. The alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (both lowercase and uppercase) are known as vowels. Alphabets except vowels are known as consonants.

Let's take an example :

#include<stdio.h>    
int main()
{    
char c;    
printf("Please enter any single character:");  
scanf("%s",&c); 
if(c=='a')
{
printf("its vowel");    
}
else if (c=='e')
{
printf("its vowel");
}
else if (c=='i')
{
printf("its vowel");
}
else if (c=='o')
{
printf("its vowel");
}
else if (c=='u')
{
printf("its vowel");
}
else
{
    printf("its consonant");
}
return 0;  
}    

Output :
Please enter any single character:a
its vowel

Output :
Please enter any single character:b
its consonant

Using Switch Case

By using switch case we also check that the inputs character is vowel or not.

Let's take an example :

#include<stdio.h>    
int main()
{    
char c;    
printf("Please enter any single character:");  
scanf("%s",&c); 
switch(c)
{
case 'a':
printf("a is  vowel");
break;
case 'e':
printf("e is  vowel");
break;
case 'i':
printf("i is  vowel");
break;
case 'o':
printf("o is  vowel");
break;
case 'u':
printf("u is  vowel");
break;
default:
printf("its consonant");
}
return 0;  
}    

Output :
Please enter any single character:i
i is vowel

Output :
Please enter any single character:b
its consonant

How do you know if a switch case is vowel or consonant?

Logic to check vowel or consonant using switch....
Input an alphabet from user. ... .
Switch the value of ch ..
For ch , there are 10 possibilities for vowel we need to check i.e. a , e , i , o , u , A , E , I , O and U ..
Write all 10 possible cases for vowels and print "Vowel" for each case ..

How do you know if an alphabet is vowel or consonant?

The five letters A , E , I , O and U are called vowels. All other alphabets except these 5 vowels are called consonants.

How do you use alphabet in switch case?

Approach.
Define a character variable ch to check the entered character..
The program is asked the user to enter an Alphabets for check whether a vowel or a consonant..
Entered character is stored in the ch variable..
Define cases for the character ch with vowel character both capital and small..

What is a switch statement in C?

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.