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 named ch 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 variable ch.
  • if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { }: Checks if the entered character ch is an alphabet. The condition (ch >= 'a' && ch <= 'z') checks if the character is a lowercase alphabet (a to z), and (ch >= 'A' && ch <= 'Z') checks if it's an uppercase alphabet (A to Z).
  • 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.