This C program checks whether a given character is a vowel or a consonant.
#include <stdio.h>
#include <stdbool.h>
int main()
{
char ch;
bool isVowel = false;
printf("Enter Character: \n");
scanf("%c",&ch);
if(ch =='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
isVowel = true;
}
if (isVowel == true)
{
printf("%c is a Vowel", ch);
}
else
{
printf("%c is a Consonant", ch);
}
return 0;
}
Result:
Enter Character:
b
b is a Consonant
Process returned 0 (0x0) execution time : 3.969 s
Press any key to continue.
One more time:
Enter Character:
e
e is a Vowel
Process returned 0 (0x0) execution time : 1.785 s
Press any key to continue.
Here's a line-by-line explanation:
#include <stdio.h>
#include <stdbool.h>
These lines include the standard input/output library header file and the header file for the bool
data type.
int main()
{
char ch;
bool isVowel = false;
This declares the main function and two variables ch
and isVowel
. ch
is a character variable to store the input character and isVowel
is a boolean variable to store whether the input character is a vowel or not.
printf("Enter Character: \n");
scanf("%c",&ch);
These lines prompt the user to enter a character and read its value from the standard input using the scanf()
function.
if(ch =='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'
||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
isVowel = true;
}
This if
statement checks if the input character is a vowel. If the character is a vowel, the boolean variable isVowel
is set to true
.
if (isVowel == true)
{
printf("%c is a Vowel", ch);
}
else
{
printf("%c is a Consonant", ch);
}
This if-else
statement checks the value of the isVowel
variable. If it is true
, the program prints that the input character is a vowel; otherwise, it prints that the input character is a consonant.
return 0;
}
This line indicates the end of the main()
function and returns 0, indicating that the program has executed successfully.
No comments:
Post a Comment