Kotlin Program to Check Whether an Alphabet is Vowel or Consonant

In this program, we will learn to code the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant. Let’s understand more about working with characters in Kotlin, then we will see the logic to check whether a character is a vowel or consonant in Kotlin Programming Language.

What are Vowels?

In english language, these 5 alphabets are known as vowels : ‘a’ , ‘e’, ‘ i ‘, ‘o’, ‘u’.

What are Consonants?

All the other Alphabets which are not Vowels are known as Consonants. For example, ‘b’, ‘c’, ‘d’, ‘z’, ‘f’, etc.

Now, let’s see the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

Kotlin Program to Check Whether an Alphabet is Vowel or Consonant

//Kotlin Program to Check Whether an Alphabet is Vowel or Consonant
import java.util.Scanner;

fun main() {
    var sc = Scanner(System.`in`);
    println("Enter an Alphabet: ");
    var ch = sc.next().single();
    
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
    println("$ch is Vowel");
    }
    
    else{
         println("$ch is Consonant");
    }
}

Output 1

Enter an Alphabet: 
a
a is Vowel

Output 2

Enter an Alphabet: b
b is Consonant

How Does This Program Work?

    var sc = Scanner(System.`in`);
    println("Enter an Alphabet: ");
    var ch = sc.next().single();

In this program, first, we have taken the input of the character from the user, using the Scanner Class of Java. We import the Scanner Class using import java.util.Scanner;

using the sc.next() method we take input of Strings in Kotlin, to take the input of a character we use next().single() method.

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
    println("$ch is Vowel");
    }
    
    else{
         println("$ch is Consonant");
    }

Then, we have used the if-else to check whether the character is one of the vowel aplhabet or not.

Now, let’s see another approach to code the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

Kotlin Program to Check Whether an Alphabet is Vowel or Consonant – Turnary If-Else

IN kotlin we can use the if-else conditions to mimic the turnary operators of other programming languages. Let’s see the code of the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

//Kotlin Program to Check Whether an Alphabet is Vowel or Consonant
import java.util.Scanner;

fun main() {
    var sc = Scanner(System.`in`);
    println("Enter an Alphabet: ");
    var ch = sc.next().single();
    
    var ans = if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') "Vowel" else "Consonant";
    
    println("$ch is $ans");

}

Output

Enter an Alphabet: 
c
c is Consonant

This program works same as explained above, but here using the if else as the turnary operator we stored the answer in a variable named ans.

There is one more approach to code the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

Kotlin Program to Check Whether an Alphabet is Vowel or Consonantusing when statement

In Kotlin Programming Language, there is a when statement which we can you to handle conditions. Here is the code of the Kotlin Program to Check Whether an Alphabet is Vowel or Consonantusing when statement.

//Kotlin Program to Check Whether an Alphabet is Vowel or Consonant
import java.util.Scanner;

fun main() {
    var sc = Scanner(System.`in`);
    println("Enter an Alphabet: ");
    var ch = sc.next().single();
    
     when(ch) {
        'a', 'e', 'i', 'o', 'u' -> println("$ch is vowel")
        else -> println("$ch is consonant")
    }
    

}

Output

Enter an Alphabet: i
i is vowel

In this we used the when statement to code the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

This is the Kotlin Program to Check Whether an Alphabet is Vowel or Consonant.

Also Read : Kotlin Program to Check Whether a Number is Even or Odd

Conclusion

I hope after going through this post, you understand how to code Kotlin Program to Check Whether an Alphabet is Vowel or Consonant. We discussed Three approaches to solve the program and check whether an alphabet is vowel or consonant using the Kotlin Programming Language.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Learn More:

Leave a Comment

Your email address will not be published. Required fields are marked *