CPP : Check Whether Number is Even or Odd



Example 1: Check Whether Number is Even or Odd using if else

#include <iostream>
using namespace std;
 
int main()
{
    int n;
 
    cout << "Enter an integer: ";
    cin >> n;
 
    if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";
 
    return 0;
}

Output

Enter an integer: 23
23 is odd.

 

Example: Check Vowel or a Consonant Manually

#include <iostream>
using namespace std;
 
int main()
{
    char c;
    int isLowercaseVowel, isUppercaseVowel;
 
    cout << "Enter an alphabet: ";
    cin >> c;
 
    // evaluates to 1 (true) if c is a lowercase vowel
    isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
 
    // evaluates to 1 (true) if c is an uppercase vowel
    isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
 
    // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    if (isLowercaseVowel || isUppercaseVowel)
        cout << c << " is a vowel.";
    else
        cout << c << " is a consonant.";
 
    return 0;
}

Output

Enter an alphabet: u
u is a vowel.


Comments