C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Decision Making Programs in C

Check whether a Character is a Vowel or Consonant

In the English alphabet, the vowels are:
a, e, i, o, u (both lowercase and uppercase).

All other alphabets (b, c, d, f, ...) are consonants.
This program will:

  1. Take a character as input.
  2. Check if it is a vowel or consonant using decision-making (if-else).
  3. Display the result.

 

C Program: Check whether a Character is a Vowel or Consonant

C

#include <stdio.h> // Include standard input/output library for printf and scanf

 

int main() {

    char ch; // Declare a character variable to store the input

 

    printf("Enter a character: "); // Prompt the user to enter a character

    scanf("%c", &ch); // Read the character input by the user

 

    // Check if the character is an alphabet (optional, but good practice)

    if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {

        printf("Error: Please enter an alphabet.\n");

        return 1; // Exit with an error code

    }

 

    // Check if the character is a vowel (lowercase or uppercase)

    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); // If it's a vowel, print this message

    } else {

        printf("%c is a consonant.\n", ch); // Otherwise, it's a consonant

    }

 

    return 0; // Indicate successful execution

}

Output

 
OUTPUT 1 :
Enter a character: a
a is a Vowel.

OUTPUT 2 :

Enter a character: K
K is a Consonant.
 

OUTPUT 3 :
 
Enter a character: 5
5 is not an alphabet. 

Explanation:

  • #include <stdio.h>: 

This line includes the standard input/output library, which provides functions like printf() for printing output to the console and scanf() for reading input from the console.

  • int main() { ... }: 

This is the main function where the program execution begins.

  • char ch;: 

A variable ch of type char is declared to store the character entered by the user.

  • printf("Enter a character: ");: 

This line displays a message on the console, prompting the user to enter a character.

  • scanf("%c", &ch);: 

This line reads a single character from the user's input and stores it in the ch variable. The %c format specifier is used for reading a character, and &ch provides the memory address of the ch variable.

  • Alphabet Check (Optional but Recommended):
    • if (!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))): This ifstatement checks if the entered character is not an alphabet (neither lowercase nor uppercase).
    • If it's not an alphabet, an error message is printed, and the program exits.
  • Vowel Check:
    • if (ch == 'a' || ch == 'e' || ... || ch == 'U'): This ifstatement checks if the ch variable is equal to any of the lowercase or uppercase vowels using the logical OR operator (||).
    • If the condition is true (the character is a vowel), printf("%c is a vowel.\n", ch);is executed, printing that the character is a vowel. 
  • Consonant Determination:
    • else { printf("%c is a consonant.\n", ch);}: If the if condition is false (meaning the character is an alphabet but not a vowel), the else block is executed, printing that the character is a consonant.
  • return 0;: 

This statement indicates that the program executed successfully.