In this tutorial, you will learning writing a C program to check input is a digit or an alphabet by using if-else statement in c. For your better understanding of the program, we have explained some examples and algorithms.
Problem statement
The problem is we have to write a C program that takes a character as input and checks whether it is a digit or an alphabet. Please see the below example to understand what is our program is all about
Example
Enter a character: 5
Output: The input is a digit.
Enter a character: A
Output: The input is an alphabet.
Enter a character: #
Output: The input is neither a digit nor an alphabet.
As you can see on the above example for the input “5” output it printed The input is a digit. Similarly for the input “A” output is printed The input is an alphabet. And for the input # output is printed The input is neither a digit nor an alphabet.
Algorithm
- Start the program.
- Declare a variable
inputof typecharto store the input character. - Print a message asking the user to enter a character.
- Read the input character using
scanfand store it in theinputvariable. - Use if-else statements to check the category of the input character:
- If
inputis between ‘a’ and ‘z’ or between ‘A’ and ‘Z’, print “The input is an alphabet.” - Else if
inputis between ‘0’ and ‘9’, print “The input is a digit.” - Else, print “The input is neither a digit nor an alphabet.”
- If
- End the program.
Program to Check a Given Input is a Digit or Alphabet
#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 if (input >= '0' && input <= '9') {
printf("The input is a digit.\n");
}
else {
printf("The input is neither a digit nor an alphabet.\n");
}
return 0;
}
Output 1
Enter a character: 4
The input is a digit
Output 2
Enter a character: r
The input is an alphabet
Program Explanation
- The program starts by including the necessary header file
stdio.h. - The
main()function is defined. - Inside the
main()function, a variableinputof typecharis declared to store the user’s input. - The program prompts the user to enter a character by printing the message “Enter a character: “.
- The
scanf()function is used to read a character from the user, and the input is stored in theinputvariable using the&operator. - The program checks the category of the input character using if-else statements:
- The first
ifstatement checks if the input is between ‘a’ and ‘z’ (lowercase alphabet) or between ‘A’ and ‘Z’ (uppercase alphabet). If true, it prints “The input is an alphabet.” - The
else ifstatement checks if the input is between ‘0’ and ‘9’ (digits). If true, it prints “The input is a digit.” - If none of the above conditions are satisfied, the
elsestatement executes and prints “The input is neither a digit nor an alphabet.”
- The first
- The
return 0statement ends the program.
Conclusion
This C program allows the user to enter a character and determines whether it is a digit or an alphabet using if-else statements. It covers all possible cases and provides the appropriate output based on the input.





