Site icon T4Tutorials.com

C++ Program: [Switch] Even or odd number using switch statement

Write a Simple program in C++ to check even or odd number using switch statement .

#include <iostream>
using namespace std;

int main() {
    int num;

    // Prompt the user to enter a number
    cout << "Enter an integer: ";
    cin >> num;

    // Use the last digit to determine even or odd
    switch (num % 2) {
        case 0:
            cout << num << " is even." << endl;
            break;
        case 1:
            cout << num << " is odd." << endl;
            break;
        default:
            cout << "Invalid input!" << endl;
            break;
    }

    return 0;
}

Example of all possible Outputs:

Example Output #1

Enter an integer:
4
4 is even.

Example Output #2

Enter an integer:
7
7 is odd.

Example Output #3

Enter an integer:
0
0 is even.

Example Output #4

Enter an integer:
-5
-5 is odd.

Example Output #5

Enter an integer:
-8
-8 is even.

Example Output #6

Enter an integer:
10
10 is even.

Exit mobile version