0
0
CProgramBeginner · 2 min read

C Program to Check Vowel or Consonant

Use a C program that reads a character and checks if it is a vowel by comparing it with 'a', 'e', 'i', 'o', 'u' (both lowercase and uppercase), else it is a consonant; for example, if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U').
📋

Examples

Inputa
Outputa is a vowel
InputB
OutputB is a consonant
Inputz
Outputz is a consonant
🧠

How to Think About It

To check if a character is a vowel or consonant, first get the character input. Then compare it with all vowels in lowercase and uppercase using or conditions. If it matches any vowel, print it is a vowel; otherwise, print it is a consonant.
📐

Algorithm

1
Get a character input from the user
2
Check if the character is one of 'a', 'e', 'i', 'o', 'u' or their uppercase versions
3
If yes, print that the character is a vowel
4
Otherwise, print that the character is a consonant
💻

Code

c
#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf(" %c", &ch);
    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
       ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') {
        printf("%c is a vowel\n", ch);
    } else {
        printf("%c is a consonant\n", ch);
    }
    return 0;
}
Output
Enter a character: a a is a vowel
🔍

Dry Run

Let's trace the input 'a' through the code

1

Input character

User inputs 'a'

2

Check vowel condition

Check if 'a' equals any vowel (a, e, i, o, u or uppercase)

3

Condition true

'a' matches 'a', so condition is true

4

Print result

Print 'a is a vowel'

StepCharacterCondition (is vowel?)Output
1atruea is a vowel
💡

Why This Works

Step 1: Input character

The program reads a single character from the user using scanf.

Step 2: Check vowels

It compares the character with all vowels in lowercase and uppercase using or conditions.

Step 3: Print result

If the character matches any vowel, it prints it is a vowel; otherwise, it prints it is a consonant.

🔄

Alternative Approaches

Using switch-case
c
#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    scanf(" %c", &ch);
    switch(ch) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            printf("%c is a vowel\n", ch);
            break;
        default:
            printf("%c is a consonant\n", ch);
    }
    return 0;
}
Switch-case improves readability by grouping vowel cases together.
Using a function to check vowels
c
#include <stdio.h>
#include <ctype.h>
int isVowel(char ch) {
    ch = tolower(ch);
    return ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u';
}
int main() {
    char ch;
    printf("Enter a character: ");
    scanf(" %c", &ch);
    if(isVowel(ch))
        printf("%c is a vowel\n", ch);
    else
        printf("%c is a consonant\n", ch);
    return 0;
}
Using a function and <code>tolower</code> reduces repeated code and handles case uniformly.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

It uses only a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time; using tolower() with a function improves readability without affecting speed.

ApproachTimeSpaceBest For
If-else conditionsO(1)O(1)Simple and direct checks
Switch-caseO(1)O(1)Better readability with grouped cases
Function with tolower()O(1)O(1)Cleaner code and case handling
💡
Use tolower() to simplify vowel checks by converting input to lowercase first.
⚠️
Forgetting to handle uppercase vowels causes wrong results for capital letters.