Vowel or Consonant: flowchart, algorithm and program
This program reads one letter into ch and prints whether it is a vowel or a consonant. It compares ch against the five vowels in both lowercase and uppercase, and anything that does not match is treated as a consonant.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Print "Enter a letter: ".
- Step 3: Read ch.
- Step 4: If ch is a, e, i, o, u (either case), go to step 5; otherwise go to step 6.
- Step 5: Print "ch is a vowel". Go to step 7.
- Step 6: Print "ch is a consonant".
- Step 7: Stop.
Pseudocode
BEGIN
PRINT "Enter a letter: "
READ ch
IF ch is a, e, i, o, u (either case) THEN
PRINT "ch is a vowel"
ELSE
PRINT "ch is a consonant"
END IF
END
Program in C, Python and Java
Vowel or Consonant program in C
#include <stdio.h>
int main() {
char ch;
printf("Enter a letter: ");
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;
}Vowel or Consonant program in Python
ch = input("Enter a letter: ")[0]
if ch in "aeiouAEIOU":
print(ch, "is a vowel")
else:
print(ch, "is a consonant")Vowel or Consonant program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a letter: ");
char ch = sc.next().charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
System.out.println(ch + " is a vowel");
else
System.out.println(ch + " is a consonant");
}
}
Sample input: ch = e
Output: e is a vowel
How it works
- scanf(" %c", &ch) reads a single character into ch. The space before %c skips any leftover newline or spaces, so the letter is read correctly.
- The if condition joins ten checks with ||: a, e, i, o, u and A, E, I, O, U. If even one of them is true, the whole condition is true.
- If the condition is true, the program prints "<ch> is a vowel". Otherwise the else branch prints "<ch> is a consonant".
- For the sample input ch = 'e', the check ch == 'e' is true, so the output is "e is a vowel".
- The program assumes the input is a letter. A digit or symbol would also be reported as a consonant, because it is not one of the ten vowel characters.
Dry run (trace table)
| Step | Line | Box | ch | Output |
|---|---|---|---|---|
| 1 | 3 | Start | ||
| 2 | 5 | Print "Enter a letter: " | Enter a letter: | |
| 3 | 6 | Read ch | e | |
| 4 | 8 | ch is a, e, i, o, u (either case)? | e | |
| 5 | 10 | Print "ch is a vowel" | e | e is a vowel |
| 6 | 14 | Stop | e |
Time and space complexity
Time: O(1) · Space: O(1). It performs a fixed number of character comparisons and uses a single char variable.
Common mistakes
- Using = instead of == in the condition. ch = 'a' assigns a value and is always true, so every input would be called a vowel.
- Forgetting the uppercase vowels, so 'E' is wrongly reported as a consonant. Either list both cases or convert ch to lowercase first.
- Writing scanf("%c", &ch) without the leading space. A newline left in the buffer from an earlier input can then be read as ch.
- Writing ch == 'a' || 'e' || 'i' and so on. Each ch == must be written out, because 'e' alone is always non-zero and makes the condition true.
Viva questions
Q1. What does this program do?
It reads one character from the user and checks whether it is one of the ten vowels, lowercase or uppercase. It prints 'vowel' if it matches and 'consonant' otherwise.
Q2. Why is there a space before %c in scanf(" %c", &ch)?
The space tells scanf to skip any whitespace such as spaces, tabs or a leftover newline before reading. Without it, %c could read a stray whitespace character instead of the letter.
Q3. Why do you use || and not && in the if condition?
A character is a vowel if it equals any one of the ten values, so the conditions are joined with OR. With && the character would have to equal all of them at once, which is impossible, so the condition would always be false.
Q4. Why do you check both lowercase and uppercase letters?
'a' and 'A' are different characters with different ASCII codes (97 and 65), so the comparison is case-sensitive. Without the uppercase checks, 'A' would be wrongly reported as a consonant.
Q5. What happens if the user enters a digit or a symbol, such as '5' or '#'?
The program prints '5 is a consonant', which is wrong. The else branch catches everything that is not a vowel, and the code never checks that the input is a letter.
Q6. How would you fix that so only letters are classified?
Add a check that the character is a letter, for example using isalpha(ch) from <ctype.h> or testing the ranges 'a' to 'z' and 'A' to 'Z'. If it is not a letter, print 'Not a letter'.
Q7. How could you shorten the condition so you don't write ten comparisons?
Convert the character to one case with tolower(ch) and compare against only five vowels. Another way is a switch statement, or strchr("aeiouAEIOU", ch).
Q8. What is the time complexity of this program?
It is O(1), constant time. It reads one character and does a fixed number of comparisons, whatever the input is.
Frequently asked questions
Why do we check both uppercase and lowercase letters?
In C, 'a' and 'A' have different character codes, so ch == 'a' does not match 'A'. Checking both cases makes the program work for any letter. You could also convert ch with tolower() and compare only five letters.
What does the space in scanf(" %c") do?
The space tells scanf to skip any whitespace, such as spaces or a leftover newline, before reading the character. Without it, scanf may read the newline instead of your letter.
What happens if I enter a digit or symbol like '5'?
It is not one of the ten vowel characters, so the else branch runs and prints "5 is a consonant". To handle this properly, first check that ch is a letter, for example with isalpha().