Skip to main content

C Program to Check Whether a Character is Vowel or Consonant

C Program to Check Whether a Character is Vowel or Consonant

In this example, if...else statement is used to check whether an alphabet entered by the user is a vowel or a constant.
 
vowels and consonants in English alphabet
To understand this example, you should have the knowledge of following C programming topics:
The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
This program assumes that the user will always enter an alphabet character.

Example #1: Program to Check Vowel or consonant

#include <stdio.h>
int main()
{
    char c;
    int isLowercaseVowel, isUppercaseVowel;

    printf("Enter an alphabet: ");
    scanf("%c",&c);

    // evaluates to 1 (true) if c is a lowercase vowel
    isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    // evaluates to 1 (true) if c is an uppercase vowel
    isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    if (isLowercaseVowel || isUppercaseVowel)
        printf("%c is a vowel.", c);
    else
        printf("%c is a consonant.", c);
    return 0;
}
Output
Enter an alphabet: G
G is a consonant.
The character entered by the user is stored in variable c.
The isLowerCaseVowel evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other character.
Similarly, isUpperCaseVowel evaluates to 1(true) if c is an uppercase vowel and 0 (false) for any other character.
If both isLowercaseVowel and isUppercaseVowel is equal to 0, the test expression evaluates to 0 (false) and the entered character is a consonant.
However, if either isLowercaseVowel or isUppercaseVowel is 1 (true), the test expression evaluates to 1 (true) and the entered character is a vowel.
The program above assumes that the user always enters an alphabet. If the user enters any other character other than an alphabet, the program will not work as intended.

Example #2: Program to Check Vowel or consonant

The program below asks the user to enter a character until the user enters an alphabet. Then, the program checks whether it is a vowel or a consonant.
#include <stdio.h>
#include <ctype.h>

int main()
{
    char c;
    int isLowercaseVowel, isUppercaseVowel;

    do {
        printf("Enter an alphabet: ");
        scanf(" %c", &c);
    }
    // isalpha() returns 0 if the passed character is not an alphabet
    while (!isalpha(c));

    // evaluates to 1 (true) if c is a lowercase vowel
    isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    // evaluates to 1 (true) if c is an uppercase vowel
    isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    if (isLowercaseVowel || isUppercaseVowel)
        printf("%c is a vowel.", c);
    else
        printf("%c is a consonant.", c);
    return 0;
}

Comments

Popular posts from this blog

C Program to Find all Roots of a Quadratic Equation

C Program to Find all Roots of a Quadratic Equation This program accepts coefficients of a quadratic equation from the user and displays the roots (both real and complex roots depending upon the discriminant ).   To understand this example, you should have the knowledge of following C programming topics: C Programming Operators C if...else Statement The standard form of a quadratic equation is: ax 2 + bx + c = 0, where a, b and c are real numbers and a ≠ 0 The term b 2 -4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. If discriminant is greater than 0, the roots are real and different. If discriminant is equal to 0, the roots are real and equal. If discriminant is less than 0, the roots are complex and different. Example: Program to Find Roots of a Quadratic Equation #include <stdio.h> #include <math.h> int main () ...

C Program to Demonstrate the Working of Keyword long

C Program to Demonstrate the Working of Keyword long The long is a size modifier, indicated by keyword long, that may increase the size of a variable during declaration. This program will demonstrate the working of long keyword.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) Example: Program to Demonstrate the Working of long #include <stdio.h> int main () { int a ; long b ; long long c ; double e ; long double f ; printf ( "Size of int = %ld bytes \n" , sizeof ( a )); printf ( "Size of long = %ld bytes\n" , sizeof ( b )); printf ( "Size of long long = %ld bytes\n" , sizeof ( c )); printf ( "Size of double = %ld bytes\n" , sizeof ( e )); printf ( "Size of long doub...

C Program to Find ASCII Value of a Character

C Program to Find ASCII Value of a Character In C programming, a character variable holds ASCII value (an integer number between 0 an 127) rather than character itself. You will learn how to find ASCII value of a character in this program.   To understand this example, you should have the knowledge of following C programming topics: C Programming Data Types C Programming Constants and Variables C Input Output (I/O) A character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself in C programming. That value is known as ASCII value. For example , ASCII value of 'A' is 65. What this means is that, if you assign 'A' to a character variable, 65 is stored in that variable rather than 'A' itself. Program to Print ASCII Value #include <stdio.h> int main () { char c ; printf ( "Enter a character: " ); // Re...