vowel or not using if-else and switch statement
In this C tutorial we are built a program to find a character is vowels or not. User can put a input we just checking it's vowels or not in both case uppercase and lowercase letters.
We now in English alphabet There are five vowels and 21 consonants in English.
, let's go
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
// Checking both lower and upper case, || is the OR operator
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c Entered character is a vowel.\n", ch);
else
printf("%c Entered character isn't a vowel.\n", ch);
return 0;
}
Theory:
In this program you are taking character input from the user (ch).
printf("Enter a character\n");
scanf("%c", &ch);
We use a if - else statement for checking vowels like 'a',' 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'. to any user input (ch) value, when user input and vowels are matched then results is printed"Entered character is a vowels" othwise else body part is expected means "Enter character isn't a vowel" msg print.
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c Entered character is a vowel.\n", ch);
else
printf("%c Entered character isn't a vowel.\n", ch);
Check vowel using switch statement:-
int main()
{
char ch;
printf("Enter a character\n");
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("%c is a vowel.\n", ch);
break;
default:
printf("%c isn't a vowel.\n", ch);
}
return 0;
}
Recommended for you
C program for swapping numbers using