Program to check : Number is Vowel or Constant
Definition & Explanation
The provided Python code defines a function isVowel(c)
that takes a character c
as input and returns True
if c
is a vowel (either uppercase or lowercase), and False
otherwise. The function first converts the input character to uppercase using c.upper()
to ensure that it matches with the uppercase vowel characters. Then, it checks if the uppercase character is equal to any of the vowels (‘A’, ‘E’, ‘I’, ‘O’, ‘U’).
Program Logic
- Convert the input character to uppercase to handle both lowercase and uppercase input.
- Check if the uppercase character is equal to any of the vowel characters (‘A’, ‘E’, ‘I’, ‘O’, ‘U’).
- If the character is a vowel, return
True
; otherwise, returnFalse
.
C
Method 1 :
#include <stdio.h>
int isVowel(char c) {
c = toupper(c); // Convert to uppercase
return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
int main() {
char c = 'e';
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
printf("Non Alphabet\n");
} else if (isVowel(c)) {
printf("%c is a Vowel\n", c);
} else {
printf("%c is a Consonant\n", c);
}
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
using namespace std;
bool isVowel(char c) {
c = toupper(c); // Convert to uppercase
return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
int main() {
char c = 'e';
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
cout << "Non Alphabet" << endl;
} else if (isVowel(c)) {
cout << c << " is a Vowel" << endl;
} else {
cout << c << " is a Consonant" << endl;
}
return 0;
}
Output :
JAVA
Method 1 :
public class Main {
public static boolean isVowel(char c) {
c = Character.toUpperCase(c); // Convert to uppercase
return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
}
public static void main(String[] args) {
char c = 'e';
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
System.out.println("Non Alphabet");
} else if (isVowel(c)) {
System.out.println(c + " is a Vowel");
} else {
System.out.println(c + " is a Consonant");
}
}
}
Output :
Python
Method 1 :
def isVowel(c):
c=c.upper()
return c =='A' or c=='E' or c=='I' or c=='O' or c=='U'
c='e'
if not c.isalpha():
print('Non Alphabet')
elif isVowel(c):
print(c,"is a Vowel")
else:
print(c, "is a consonant")
Output :
e is a Vowel