20. C language program to check if a given character is a special character of digit using if else statement.
Purpose:
The purpose of the program is to determine whether a given character is a special character or a digit. This can be useful in data validation processes, input filtering, and character classification tasks in programming.
Agenda:
1. Define a variable to store the input character.
2. Prompt the user to enter a character.
3. Check if the entered character is a special character by comparing it with a predefined list of special characters using 'if-else' statements.
4. If the character is not a special character, check if it is a digit using 'if-else' statements.
5. Display the result to the user indicating whether the input character is a special character or a digit.
Real-life applications:
In a text processing application, this program can be used to identify and handle special characters differently from digits. For example, in a password validation system, special characters may have specific requirements compared to digits. By categorizing characters accurately, the program can enforce the necessary rules for data input.
A program in C language to check if a given character is a special character of digit using if else statement.
/* C language program to to check if a given character is a
special character of digit using if else statement.*/
#include <stdio.h>
int main()
{
char ch;
printf("\nEnter a character: ");
scanf("%c", &ch);
if(ch >= '0' && ch <= '9')
{
printf("\n%c is a digit.\n", ch);
} else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("\n%c is a special character.\n", ch);
} else
{
printf("\n The character %c is neither a special character nor a digit.\n", ch);
}
return 0;
}
Output
Enter a character:
10
10 is a digit.