java program to check whether a character is alphabet or not
Write a java program to check whether a character is alphabet or not
Here, the char variable stores the ASCII value of a character (number between 0 and 127) rather than the character itself.
The ASCII value of lowercase alphabets are from 97 to 122. And the ASCII value of uppercase alphabets are from 65 to 90. That is, alphabet a is stored as 97 and alphabet z is stored as 122. similarly, alphabet A is stored as 65 and alphabet Z is stored as 90.
Now, when we compare variable c between 'a' to 'z' and 'A' to 'Z', the variable is compared with the ASCII value of the alphabets 97 to 122 and 65 to 90 respectively.
since the ASCII value of * does not fall in between the ASCII value of alphabets. Hence, the program outputs * is not an alphabet.
- Program:
public class Alphabet {
public static void main(String[] args) {
char c = '-';
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println(c + " is an alphabet.");
else
System.out.println(c + " is not an alphabet.");
}
}
- Output:
- is not an alphabet.----- OR -----n is an alphabet.----- OR -----V is an alphabet.
- Visit:
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here
Write a java program to multiply two floating point numbers Here
Java program to check whether a number is positive or negative Here
0 comments