C Program to check whether a character is a vowel or consonant (Assuming that user will enter an alphabet)

by | Jan 9, 2023 | C, C Programs

Introduction

In this program, we will create a C Program which will include one input from the user in the form of a character, and we will show output if the entered

To understand this example, you should know about

1. C programming operators

2. if…else statement

Vowels include both lower characters, i.e., a, e, i, o, u, and the upper characters, i.e., A, E, I, O, or U.

All other alphabets, including lower case and upper case, except vowels, are consonants.

Program

#include<stdio.h>
int main()
{
char sChar;
printf("Enter a character:");
scanf("%c",&sChar);


if ( (sChar=='a' || sChar=='e'|| sChar=='i'|| sChar=='o'|| sChar=='u') ||
(sChar=='A' || sChar=='E'|| sChar=='I'|| sChar=='O'|| sChar=='U') ) // ‘||’(or) operator is used
{
printf("Character %c is a vowel",sChar);
}
else
{
printf("Character %c is a consonent",sChar);
}
return 0;
}

 

Output

Explanation

The character value entered by the user is ‘o’.

if ( (ch==’a’ || ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’) ||

(ch==’A’ || ch==’E’|| ch==’I’|| ch==’O’|| ch==’U’) )

In the above ‘if’ statement we split the condition into 2 parts to make the code clear.

(ch==’a’ || ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’) this part of the condition checks for lower case vowels using the ‘||’ operator. If any one matches with the value of ‘ch’ it will give true result.

(ch==’A’ || ch==’E’|| ch==’I’|| ch==’O’|| ch==’U’) this part of the condition checks for upper case vowels.

Since the value of ‘ch’ matches with one of the characters written in the if statement, thus its body gets executed and

‘Character o is a vowel’ is displayed on the output screen.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.