Write ac program to check whether the entered character is vowel or not use switch case

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System; class Program { static void Main(string[] args) { char ch; //Reading an alphabet from user Console.WriteLine("Enter any alphabet: "); ch = Convert.ToChar(Console.ReadLine()); // checking vowel and consonant switch (ch) { case 'a': Console.WriteLine("vowel"); break; case 'e': Console.WriteLine("vowel"); break; case 'i': Console.WriteLine("vowel"); break; case 'o': Console.WriteLine("vowel"); break; case 'u': Console.WriteLine("vowel"); break; case 'A': Console.WriteLine("vowel"); break; case 'E': Console.WriteLine("vowel"); break; case 'I': Console.WriteLine("vowel"); break; case 'O': Console.WriteLine("vowel"); break; case 'U': Console.WriteLine("vowel"); break; default: Console.WriteLine("consonant"); break; } Console.ReadLine(); } }
Write C# program to check vowel or consonant using switch case

Below is a program to check vowel using switch case.

#include<stdio.h> int main() { printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); char ch; printf("Input a Character : "); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("\n\n%c is a vowel.\n\n", ch); break; default: printf("%c is not a vowel.\n\n", ch); } printf("\n\n\t\t\tCoding is Fun !\n\n\n"); return 0; }

Output:


Explanation:

  • If break statement is not used for a case then all the cases following the valid case are executed and evaluated. This way you can make your code easier to understand by writing only break statement only once to check multiple conditions in one go.
  • default is executed only if none of the above cases are true. It is similar to the else statement of the if-else code.

  • Write a C program to check whether a character is vowel or consonant using switch case statement.
  • How to find whether an alphabet is vowel or not using switch case statement.


  • C printf and scanf functions
  • Switch case statement in C

A vowel is the alphabets that represents a speech sound created by the relatively free passage of breath through the larynx and oral cavity. Letters that are not vowels are consonants. English has five proper vowel letters (A, E, I, O, U) all alphabets except these characters are consonants.

#include <stdio.h> int main() { char c; /* * Take a character as input form user */ printf("Enter an Alphabet\n"); scanf("%c", &c); /* Check If input alphabet is vowel or not using switch statement */ switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is VOWEL", c); break; default: printf("%c is CONSONANT", c); } return 0; } Output Enter an Alphabet e e is VOWEL Enter an Alphabet Z Z is CONSONANT
Related Topics

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

This program assumes that the user will always enter an alphabet character.

Program to Check Vowel or consonant

#include <stdio.h> int main() { char c; int lowercase_vowel, uppercase_vowel; printf("Enter an alphabet: "); scanf("%c", &c); // evaluates to 1 if variable c is a lowercase vowel lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 if variable c is a uppercase vowel uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // evaluates to 1 (true) if c is a vowel if (lowercase_vowel || uppercase_vowel) printf("%c is a vowel.", c); else printf("%c is a consonant.", c); return 0; }

Output

Enter an alphabet: G G is a consonant.

The character entered by the user is stored in variable c.

The lowercase_vowel variable evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other characters.

Similarly, the uppercase_vowel variable evaluates to 1 (true) if c is an uppercase vowel and 0 (false) for any other character.

If either lowercase_vowel or uppercase_vowel variable is 1 (true), the entered character is a vowel. However, if both lowercase_vowel and uppercase_vowel variables are 0, the entered character is a consonant.

Note: This program assumes that the user will enter an alphabet. If the user enters a non-alphabetic character, it displays the character is a consonant.

To fix this, we can use the isalpha() function. The islapha() function checks whether a character is an alphabet or not.

#include <ctype.h> #include <stdio.h> int main() { char c; int lowercase_vowel, uppercase_vowel; printf("Enter an alphabet: "); scanf("%c", &c); // evaluates to 1 if variable c is a lowercase vowel lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 if variable c is a uppercase vowel uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // Show error message if c is not an alphabet if (!isalpha(c)) printf("Error! Non-alphabetic character."); else if (lowercase_vowel || uppercase_vowel) printf("%c is a vowel.", c); else printf("%c is a consonant.", c); return 0; }

Now, if the user enters a non-alphabetic character, you will see:

Enter an alphabet: 3 Error! Non-alphabetic character.

In this article, we will discuss the concept of the C program to Check Vowel or consonant using switch case statements

In this post, we are going to learn how to check the vowels and consonants using switch statements in C  programming language

Check vowel and consonants

C program to check Vowel or consonant using switch case with the break

The program allows to enter an Alphabet and it checks and displays whether the given alphabet is vowel or consonant with the break statements

Program 1

#include <stdio.h> #include <stdlib.h> int main() { char ch; printf("Enter any Alphabet\n"); //input alphabet from user scanf("%c",&ch);//store the Entered Alphabet in ch switch(ch){ //check lower case vowel letters case 'a': printf("%c is a vowel",ch); break; case 'e': printf("%c is a vowel",ch); break; case 'i': printf("%c is a vowel",ch); break; case 'o': printf("%c is a vowel",ch); break; case 'u': printf("%c is a vowel",ch); break; //check upper case vowel letters case 'A': printf("%c is a vowel",ch); break; case 'E': printf("%c is a vowel",ch); break; case 'I': printf("%c is a vowel",ch); break; case 'O': printf("%c is a vowel",ch); break; case 'U': printf("%c is a vowel",ch); break; default: printf("%c is a consonant",ch); break; } getch(); return 0; }

When the above code is executed, it produces the following result

case 1

Enter any Alphabet a a is a vowel

case 2

Enter any Alphabet E E is a vowel

case 3

Enter any Alphabet M M is a consonant

case 4

Enter any Alphabet y y is a consonant

Approach

  • Define a character variable ch
  • The program is asked the user to enter an Alphabets to check whether vowel or consonant
  • Entered character is stored in the ch variable
  • Define cases for the character ch with vowel character both capital and small
  • Next, the program checks every case using the given character.
  • Finally, it displays whether the given character is vowel or consonant

C program to check Vowel or consonant using switch case without the break

The program allows to enter an Alphabet and it checks and displays whether the given alphabet vowel or consonant without the break statements

Program 2

#include <stdio.h> #include <stdlib.h> int main() { char ch; printf("Enter any Alpabet\n"); //input alphabet from user scanf("%c",&ch);//store the Entered Alphabet in ch switch(ch){ //check lower case vowel letters case 'a': case 'e': case 'i': case 'o': case 'u': //check upper case vowel letters case 'A': case 'E': case 'I': case 'O': case 'U': printf("%c is a vowel",ch); break; default: printf("%c is a consonant",ch); break; } getch(); return 0; }

When the above code is executed, it produces the following result

case 1

Enter any Alphabet e e is a vowel

case 2

Enter any Alphabet U U is a vowel

case 3

Enter any Alphabet G G is a consonant

case 4

Enter any Alphabet r r is a consonant

Approach

  • Define a character variable ch to check the entered character
  • The program is asked the user to enter an Alphabets to check whether vowel or consonant
  • Entered character is stored in the ch variable
  • Define cases for the character ch with vowel character both capital and small
  • Next, the program checks every case using the given character.
  • Finally, it displays whether the given character is vowel or consonant

Suggested for you

Data type in C language

Variable in C language

The operator in C language

Switch case statements in C language

Similar post

Java program to check Vowel or consonant using switch case statements

C++ program to Check Vowel or consonant using switch case statements

Postingan terbaru

LIHAT SEMUA