In this tutorial, you will learn how to write a program in C to check whether a given input is an alphabet or not. We will be using an if-else statement to write our program. We have covered some examples, algorithm, Program, and explanations so that you can understand this tutorial very well.
Problem Statement
Our aim is to write a C program that checks whether a given input character is an alphabet or not using if-else statements. Our program should take an input character from the user. After taking character it should check and print the output accordingly. Like if the input is alphabet then it will print the “The input is an alphabet”. In the other case, it will print “The input is not an alphabet”.
Example
Enter a character: T
Output: The input is an alphabet.
Enter a character: 5
Output: The input is not an alphabet.
Algorithm
- Declare a variable
input
of type char to store the user input. - Display a prompt asking the user to enter a character.
- Read the character entered by the user and store it in the
input
variable. - Use an if-else statement to check if
input
falls within the range of lowercase letters ‘a’ to ‘z’ or uppercase letters ‘A’ to ‘Z’. - If the condition is true, print “The input is an alphabet.”
- If the condition is false, print “The input is not an alphabet.”
- Return 0 to indicate successful execution of the program.
Program to check whether a given input is an alphabet or not
#include <stdio.h>
int main() {
char input;
printf("Enter a character: ");
scanf("%c", &input);
if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z')) {
printf("The input is an alphabet.\n");
} else {
printf("The input is not an alphabet.\n");
}
return 0;
}
Output 1
Enter a character: 5
The input is not an alphabet
Output 2
Enter a character: Z
The input is an alphabet
Program Explanation
This program checks whether a given input character is an alphabet or not using if-else statements.
The program starts by declaring a variable input
of type char to store the user input. It prompts the user to enter a character and reads the input using the scanf
function, storing it in the input
variable.
Next, an if-else statement is used to check if the input character is an alphabet. The condition (input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z')
checks if the input character falls within the range of lowercase letters ‘a’ to ‘z’ or uppercase letters ‘A’ to ‘Z’. If the condition is true, it prints “The input is an alphabet.” Otherwise, it prints “The input is not an alphabet.”
Finally, the program returns 0 to indicate successful execution.
This program provides a simple way to determine whether a given input character is an alphabet or not.
Conclusion
The C program presented above allows the user to enter a character and determines whether it is an alphabet or not using if-else statements. It provides a simple and effective method to identify alphabet characters based on their ASCII values.