Table of Contents
Introduction
In this C program, we will explore how to check if a character is an alphabet or not.
The user is asked to enter a character value to check whether it is an alphabet or a non-alphabet character.
To understand this program, you should know about
1. C programming operators
2. C nested if…else statement
We have three ways to check whether a character is an alphabet or a non-alphabet.
In this program, we will use the alphabet to check the given condition.
Program
#include<stdio.h> #include<math.h> int main() { char c; printf("Enter a character:\n"); scanf("%c",&c); if((c>='a'&& c<='z') || (c>='A'&& c<='Z')) //all alphabets whether lower case or upper case lies in between these characters { printf("%c is an alphabet",c); } else { printf("%c is not an alphabet",c); } return 0; }
Output
Explanation
The character value entered by the user is stored in variable ‘c’.
if((c>=’a’&& c<=’z’) || (c>=’A’&& c<=’Z’))
There are two conditions written in above if statement i.e., (c>=’a’&& c<=’z’) and (c>=’A’&& c<=’Z’). If either of them is true then the body of ‘if’ will be executed because they are operated with ‘||’ operator.
In the first output, the character is ‘v’ which lies in between ‘a to z’ thus, ‘v’ is an alphabet.
In the second output, the character is ‘&’ which neither lies in-between ‘a to z’ nor in-between ‘A to Z’. Thus, ‘&’ is not an alphabet.
0 Comments