Checking for Vowels in C using the Ternary Operator


This is a C program that prompts the user to enter a character, and then uses the ternary operator (? :) to check if it is a vowel. If it is a vowel, it prints a message saying "This character is a vowel: [value of ch]" and if it is not a vowel, it prints a message saying "This character is not a vowel: [value of ch]" .

The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution. Inside the main function, a character variable ch is declared. printf() is used to display the prompt for the user to enter a character, and scanf() is used to get the input and store it in the variable 'ch'.

The ternary operator ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'?printf("This character is a vowel:%c",ch):printf("This character is not a vowel:%c",ch); is used to check if the entered character is a vowel or not. The condition being checked here is ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u', which returns true if the entered character is 'a', 'e', 'i', 'o' or 'u' and false if it's not. The operator || is used to check the multiple conditions, it will be true if any one of the conditions is true. If the entered character is a vowel, the first part of the ternary operator, printf("This character is a vowel:%c",ch), will execute, else the second part of the ternary operator printf("This character is not a vowel:%c",ch) will execute.

Finally, the return 0 statement is used to indicate the successful execution of the program. The return value of 0 is a convention used to indicate that the program has executed correctly.

Source Code

#include<stdio.h>
int main()
{
    char ch,a,e,i,o,u;
    printf("\nEnter the character:");
    scanf("%c",&ch);
    ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'?printf("This character is a vowel:%c",ch):printf("This character is not a vowel:%c",ch);
    return 0;
}
To download raw file Click Here

Output

Enter the character : o 
This character is a vowel:o

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C