You are currently viewing C Program To Check Whether A Character Is A Vowel Or Consonant Using If-else

C Program To Check Whether A Character Is A Vowel Or Consonant Using If-else

In this tutorial, you will learn to write a program to check whether a character is a vowel or constant using if-else. Here, we are using examples, and algorithms with fully explained programs for your better understanding of this problem. This topic is also suited for checking whether an input character is a vowel or consonant in the C program using if-else.

Problem Statement

We have to Write a C program that checks whether a given character is a vowel or a consonant. Our program should take a character as input and after checking they should spring a character as a vowel or consonant.

Example

Enter a character: A
Output: The character A is a vowel.

Enter a character: Z
Output: The character Z is a consonant.

Algorithm to Check Character Is A Vowel Or Consonant Using If-else

  1. Start the program.
  2. Declare a variable character of type char to store the input character.
  3. Prompt the user to enter a character by printing the message “Enter a character: “.
  4. Read the input character from the user and store it in the character variable using scanf().
  5. Convert the character to uppercase using the toupper() function to handle lowercase inputs.
  6. Use an if-else statement to check if the input character is a vowel or a consonant:
    • If character is equal to ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’, print the message “The character [character] is a vowel.”
    • Otherwise, print the message “The character [character] is a consonant.”
  7. End the program.

C Program to check whether a character is a vowel or consonant

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

Output 1

Enter a character: A
The character A is a vowel

Output 2

Enter a character: S
The character S is a consonant

Program Explanation

  • The program starts by including the necessary header files: stdio.h for input/output functions and ctype.h for character handling functions.
  • The main() the function is defined.
  • Inside the main() function, a variable character of type char is declared to store the input character.
  • The program prompts the user to enter a character by printing the message “Enter a character: “.
  • The scanf() function is used to read a character from the user, and the input is stored in the character variable using the %c format specifier. A space is added before %c in scanf to consume any leading whitespace or newline characters from the previous input.
  • The toupper() function is used to convert the input character to uppercase to handle lowercase inputs.
  • An if-else statement is used to check if the input character is a vowel or a consonant:
    • If the input character is ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’, the program prints the message “The character [character] is a vowel.” The %c format specifier is used in printf() to print the character.
    • If the input character is not one of the vowels, the program prints the message “The character [character] is a consonant.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter a character and determines whether it is a vowel or a consonant using if-else statements. It covers all possible cases and provides the appropriate output based on the input character.