The program segment given below uses a nested if statement to determine whether a given character is a letter or not. In addition, if the given character is a letter, it tests whether it is a vowel or consonant.
ch= getchar();
if (ch >= ‘A’ && ch <= ‘Z’ || ch >=’a’ && ch <= ‘z’ ) {
/* ch is a letter, test for vowel */
if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch== ‘O’ || ch== ‘U’ || ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch== ‘o’ || ch== ‘u’ )
printf(“%c is vowel\n”, ch);
else printf(“%c is consonant\n”, ch);
}
else printf(“%c is not a letter\n”, ch);
The outer if statement first tests whether the given character ch is a letter or not. If ch is a letter (either uppercase or lowercase),the condition
if (ch >= ‘A’ && ch <= ‘Z’ || ch >=’a’ && ch <= ‘z’ )
evaluates as true and inner if-else statement determines and prints whether ch is a vowel or aconsonant; otherwise, the else block prints that the given character is not a letter.
#include <stdio.h>
void main() {
char ch;
clrscr();
printf(“Enter a Character :\n”);
ch= getchar();
if (ch >= ‘A’ && ch <= ‘Z’ || ch >=’a’ && ch <= ‘z’ ) {
/* ch is a letter, test for vowel */
if (ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch== ‘O’ || ch== ‘U’ || ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch== ‘o’ || ch== ‘u’ )
printf(“%c is vowel\n”, ch);
else printf(“%c is consonant\n”, ch);
}
else printf(“%c is not a letter\n”, ch);
getch();
}