Check Whether a Character is Vowel or Consonant | faceprep

Given a character, to find out if that character is a vowel or consonant can be done in two ways.

Method 1: Using if-else, a given character can be categorized as a vowel or consonant.

Method 2: using switch-case, a given character can be categorized as a vowel or consonant.

Program to check if the given character is a vowel or consonant using if-else

// c program to check vowel or consonant using if else

#include <stdio.h>

void vowel_or_consonant(char ch)
{
if ((ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’) || (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’))
printf(“Vowel\n”);
else
printf(“Consonant\n”);
}

int main()
{
char ch;
printf(“\nInput a character : “);
scanf(” %c”,&ch);
printf(“\n%c is a “,ch);
vowel_or_consonant(ch);
return 0;
}

// c++ program to check vowel or consonant using if else

#include
using namespace std;

void vowel_or_consonant(char ch)
{
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ || ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’)
cout << “Vowel” << endl;
else
cout << “Consonant” << endl;
}

int main()
{
char ch;
cout << “\nInput a character : “;
cin >> ch;
vowel_or_consonant(ch);
return 0;
}

// Program to check vowel or consonant in java

import java.util.*;

public class Main {

static void vowel_or_consonant(char ch)
{
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ || ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’)
System.out.println(“Vowel”);
else
System.out.println(“Consonant”);
}

public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the character :”);
char ch = sc.next().charAt(0);
vowel_or_consonant(ch);
}
}

# Python program to check if a character is a vowel or a consonant

def vowel_or_consonant(ch):
if (ch == ‘a’ or ch == ‘e’ or ch == ‘i’ or ch == ‘o’ or ch == ‘u’ or ch == ‘A’ or ch == ‘E’ or ch == ‘I’ or ch == ‘O’ or ch == ‘U’):
print(“Vowel”)
else:
print(“Consonant”)

ch = input(“Enter a character : “)
vowel_or_consonant(ch)

Program to check if the given character is a vowel or consonant using switch case

// C program to check if the character is a vowel or consonant using switch case

#include <stdio.h>

void vowel_or_consonant(char ch)
{
switch(ch)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’: printf(“Vowel\n”);
break;
default : printf(“Consonant\n”);
}
}

int main()
{
char ch;
printf(“\nInput a character : “);
scanf(” %c”,&ch);
printf(“\n%c is a “,ch);
vowel_or_consonant(ch);
return 0;
}

// C++ program to check vowel or consonant using switch  case


#include <iostream>
using namespace std;

void vowel_or_consonant(char ch)
{
switch(ch)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’: cout << “Vowel” << endl;
break;
default : cout << “Consonant” << endl;
}

}

int main()
{
char ch;
cout << “\nInput a character : “;
cin >> ch;
vowel_or_consonant(ch);
return 0;
}

// Program to check vowel or consonant in java

import java.util.*;

public class Main {

static void vowel_or_consonant(char ch)
{
switch(ch)
{
case ‘a’:
case ‘A’:
case ‘e’:
case ‘E’:
case ‘i’:
case ‘I’:
case ‘o’:
case ‘O’:
case ‘u’:
case ‘U’: System.out.print(“Vowel\n”);
break;
default : System.out.print(“Consonant\n”);
}
}


public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the character :”);
char ch = sc.next().charAt(0);
vowel_or_consonant(ch);
}
}

vowel or consonant

vowel or consonant