c program for determining a character is alphabet or not
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is an alphabet.\n", ch);
} else {
printf("%c is not an alphabet.\n", ch);
}
return 0;
}
Explanation:
#include <stdio.h>
: Includes the standard input-output header file for input/output operations.int main() { }
: The main function where the program execution begins.char ch;
: Declares a character variable namedch
to store user input.printf("Enter a character: ");
: Displays a message asking the user to input a character.scanf("%c", &ch);
: Reads the input character provided by the user and stores it in the variablech
.if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { }
: Checks if the entered characterch
is an alphabet. The condition(ch >= 'a' && ch <= 'z')
checks if the character is a lowercase alphabet (a
toz
), and(ch >= 'A' && ch <= 'Z')
checks if it's an uppercase alphabet (A
toZ
).printf("%c is an alphabet.\n", ch);
: Prints a message indicating that the entered character is an alphabet if the condition is true.else { printf("%c is not an alphabet.\n", ch); }
: If the condition is false, it prints a message indicating that the entered character is not an alphabet.return 0;
: Indicates successful execution and termination of the program.