## C Program To Check A Given Input Is A Digit Or Alphabet Using If-else

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

1. Start the program.
2. Declare a variable `input` of type `char` to store the input character.
3. Print a message asking the user to enter a character.
4. Read the input character using `scanf` and store it in the `input` variable.
5. Use if-else statements to check the category of the input character:
• If `input` is between ‘a’ and ‘z’ or between ‘A’ and ‘Z’, print “The input is an alphabet.”
• Else if `input` is between ‘0’ and ‘9’, print “The input is a digit.”
• Else, print “The input is neither a digit nor an alphabet.”
6. 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 variable `input` of type `char` is 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 the `input` variable using the `&` operator.
• The program checks the category of the input character using if-else statements:
• The first `if` statement 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 if` statement 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 `else` statement executes and prints “The input is neither a digit nor an alphabet.”
• The `return 0` statement 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.