In Java program:
Write a program that takes a character from the user and classifies it as a number (‘1’,’2’, ‘3’,4, 5, 6, 7 …), a letter from the alphabet (‘A’, ‘a’, ‘B’, ‘b’, …, ‘Z’, ‘z’), an arithmetic operator (‘+’, ‘-‘, ‘/’, ‘*’, ‘%’), a comparison operator (‘<’, ‘>’, ‘=’), punctuation (‘!’, ‘?’, ‘,’, ‘,’, ‘:’, ‘;’), and all other characters as a Special Character, and informs the user of the same.
If the user entered the character A, the system should say: “You entered a letter!”
If the user entered the character (, the system should say: “You entered a special character!”
//CharacterClassifier.java import java.util.Scanner; public class CharacterClassifier { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); if(ch>='0' && ch<='9'){ System.out.println("You entered a number!"); } else if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')){ System.out.println("You entered a letter!"); } else if(ch=='+' || ch=='-' || ch=='/' || ch=='*' || ch=='%'){ System.out.println("You entered an arithmetic operator!"); } else if(ch=='<' || ch=='>' || ch=='='){ System.out.println("You entered a comparison operator!"); } else if(ch=='!' || ch=='?' || ch==',' || ch==':' || ch==';'){ System.out.println("You entered a punctuation !"); } else{ System.out.println("You entered a special character!"); } } }
Get Answers For Free
Most questions answered within 1 hours.