When you're diving into C, checking if a character is a vowel or consonant is a classic beginner task. For example, type in 'a' and the program should say it's a vowel. But enter 'b', and you'll get a consonant. Let's see how we do this with if-else statements.
Prerequisites for the Vowel or Consonant Program in C
Before we start coding, make sure you understand these concepts:
- The if-else statement in C for decision making
- Character handling using
chardata type - Input/Output functions like
scanfandprintf - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Check Whether a Character is a Vowel or Consonant
Below is the full C code to check if a character is a vowel or consonant.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
// Check if the character is a vowel
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
printf("The character is a vowel.\n");
} else {
printf("The character is a consonant.\n");
}
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Vowel or Consonant Program
Here's what you should see when you enter 'a' as input:
Enter a character: a
The character is a vowel.
Algorithm to Check Vowel or Consonant in C
The program reads a character and checks if it's a vowel by comparing it against known vowel characters. Here is how it works step by step:
- Read the character from the user.
- Check if the character is equal to any of the vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U').
- If it matches, print "The character is a vowel."
- If not, print "The character is a consonant."
flowchart TD
A([Start]) --> B[/Read character/]
B --> C{Is character vowel?}
C -->|Yes| D[Print "Vowel"]
C -->|No| E[Print "Consonant"]
D --> F([End])
E --> FHow the C Program to Check Vowel or Consonant Works
Let's break down the code to understand what each part does.
#include <stdio.h>: Includes the standard input-output library necessary forprintfandscanf.char ch;: Declares a character variable to store user input.printf("Enter a character: ");: Prompts the user to enter a character.scanf(" %c", &ch);: Reads the input character.- The
ifstatement checks ifchis any of the vowels (both uppercase and lowercase). - If true,
printf("The character is a vowel.\n");outputs that it's a vowel. - Otherwise,
printf("The character is a consonant.\n");indicates it's a consonant. return 0;: Indicates successful program termination.
Quick Quiz — test your understanding
Tap an option to check your answerWhat is a potential issue if the input character is uppercase?
How Fast Is the Vowel or Consonant Program in C?
The time and space complexity of this program are straightforward.
Time Complexity: O(1). The program checks a constant number of conditions (10 vowels) regardless of input, like if conditions for each vowel.
Space Complexity: O(1). It uses only one character variable ch, irrespective of input size.
Common mistake: Be careful with uppercase and lowercase vowels; ensure both are checked.
Key takeaway: Always think about case sensitivity when working with character data.
FAQs About Checking Vowel or Consonant in C
Here are some common questions students have about this program.
if checks, like 'A', 'E', 'I', 'O', 'U'.switch statement with cases for each vowel works.Related Programs
If you found this program helpful, check out these related programs:
- C Program to Find the Frequency of Characters in a String
- C Program to Find ASCII Value of a Character
- C Program to Display Characters from A to Z Using Loop
- C Program to Sort Elements in Lexicographical Order
- C Program to Count Vowels and Consonants in a String
Conclusion
Think about extending this code to handle strings, not just single characters. It pops up in text-processing apps all the time. You might also want to explore string manipulation in C next. This opens up more advanced C programming tasks.
For more practice, check out the C programming quiz or write and test your code in the C online compiler.