Q:

Scala program to check the given character is vowel and consonant

belongs to collection: Scala Pattern Matching Programs

0

Here, we will read a character from the user and check given character is vowel or consonant.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to check the given character is vowel and consonant is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to check the given character 
// is vowel and consonant

object Sample {
    def main(args: Array[String]) {  
        var ch:Char=0;
    
        print("Enter character: ")  
        ch=scala.io.StdIn.readChar()
        
        ch match{
            case 'A'=>printf("%c is a VOWEL.\n",ch);
            case 'E'=>printf("%c is a VOWEL.\n",ch);
            case 'I'=>printf("%c is a VOWEL.\n",ch);
            case 'O'=>printf("%c is a VOWEL.\n",ch);
            case 'U'=>printf("%c is a VOWEL.\n",ch);
            case 'a'=>printf("%c is a VOWEL.\n",ch);
            case 'e'=>printf("%c is a VOWEL.\n",ch);
            case 'i'=>printf("%c is a VOWEL.\n",ch);
            case 'o'=>printf("%c is a VOWEL.\n",ch);
            case 'u'=>printf("%c is a VOWEL.\n",ch);
            case _=>printf("%c is a CONSONANT.\n",ch);
        }  
    }
}  

Output:

Enter character: E
E is a VOWEL.

Explanation:

In the above program, we used an object-oriented approach to create the program. Here, we created an object Sample. We defined main() function. The main() function is the entry point for the program.

In the main() function, we created a  character variable ch initialized with 0. Then we read the value of ch from the user. Here, we used the match case to check vowels. If the given character is not matched with any cases. Then it will be treated as a consonant.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala program to implement an arithmetic calculato... >>
<< Scala program to read a weekday number and print w...