Menu Close

Go – Program to find if a character is vowel or Consonant in go

Here, we will see program to find if a character is vowel or Consonant in go. Given below, you can find algorithm and program.

Algorithm:

  • Get the character input from user
  • Check this character whether It is vowel or consonant
  • Return the result
Input: char = 'a'
Output: 'Vowel' 

Input: char = 'x'
Output: 'Consonant' 

Input: char = 'e'
Output: 'Vowel' 

Code:

package main

import (

  "fmt"

)

func vowelOrConsonant(char rune) string  {

  if ((char == 'a') || (char == 'e') || (char == 'i') ||
      (char == 'o') || (char == 'u')) {

    return "Vowel"

  } else {

    return "Consonant"

  }

}

func main() {

  x := vowelOrConsonant('a')

  fmt.Println(x)



  x = vowelOrConsonant('x')

  fmt.Println(x)



  x = vowelOrConsonant('e')

  fmt.Println(x)

}

Output:

Vowel

Consonant

Vowel

Using switch case:

package main

import (

  "fmt"

)

func vowelOrConsonant(char rune) string  {

  switch(char) {

    case 'a', 'e', 'i', 'o', 'u':

       return "Vowel"

    default:

      return "Consonant"

  }
}

func main() {

  x := vowelOrConsonant('a')

  fmt.Println(x)



  x = vowelOrConsonant('x')

  fmt.Println(x)



  x = vowelOrConsonant('e')

  fmt.Println(x)

}

Output:

Vowel

Consonant

Vowel

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, golang program

Leave a Reply

Your email address will not be published.

Contact Us