Scala program to check the given character is vowel and consonant

Here, we are going to learn how to check the given character is vowel and consonant in Scala programming language?
Submitted by Nidhi, on April 28, 2021

Problem Solution:

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

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.

Scala Pattern Matching Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.