Examples of Switch Case in C++

Switch Case Statement

Switch case is used for control statements which decides the execution of statements rather than if-else statements. In other word, Switch Case is utilized when there are several cases and each requires a different task to be completed.

context switching in operating system

Examples of Switch Case statement in C++

Syntax:

switch(expression)
{
case 1:
      statement(s);
break;
case 2:
      statement(s);
break;
default:
      statement(s);
}

Problem 1:

Write a Program using switch case for performing operation on following conditions.

  • If operator is ‘+’, then print addition of given integer.
  • If operator is ‘-‘, then print subtraction of given integer.
  • If operator is ‘*’, then print multiplication of given integer.
  • If operator is ‘%’, then print division of given integer.
Run
#include<iostream>
using namespace std;
int main()
{
  int num1, num2;
  char op;
  cout << "Enter two numbers and an operator (+, -, *, %): "; cin >> num1 >> num2 >> op;
  switch (op)
    {
    case '+':
      cout << num1 << " + " << num2 << " = " << num1 + num2;
      break;
    case '-':
      cout << num1 << " - " << num2 << " = " << num1 - num2;
      break;
    case '*':
      cout << num1 << " * " << num2 << " = " << num1 * num2;
      break;
    case '%':
      cout << num1 << " % " << num2 << " = " << num1 % num2;
      break;
    default:
      cout << "Invalid operator";
    }
  return 0;
}

Output

Enter two numbers and an operator (+, -, *, %): 11 13 *  
11 * 13 = 143

Problem 2:

Write a program using switch case to check the CGPA on given condition.

  • If the CGPA is 8 then print You got 8 CGPA.
  • If the CGPA is 10 then print You got 10 CGPA.
  • If the CGPA is 7 then print You got 7 CGPA.
Run
#include<iostream> 
using namespace std;
int main() {
    double cgpa;
    cout << "Enter your CGPA: "; cin >> cgpa;
    switch ((int)cgpa) {
        case 8:
            cout << "You got 8 CGPA.";
            break;
        case 7:
            cout << "You got 7 CGPA.";
            break;
        case 10:
            cout << "You got 10 CGPA.";
            break;
        default:
            cout << "Invalid CGPA";
    }
    return 0;
}

Output

Enter your CGPA: 10
You got 10 CGPA. 

Problem 3:

Write a program using switch case for printing that you are eligible for voting or not.

  • If age is equal to 18 then you are not eligible for voting.
  • If age is equal to 20 then you are eligible for voting.
Run
#include<iostream>
using namespace std;
int main() {
    int age;
    cout << "Enter your age: "; cin >> age;
    switch (age) {
        case 18:
            cout << "You are not eligible for voting.";
            break;
        case 20:
            cout << "You are eligible for voting.";
            break;
        default:
            if (age < 18) {
                cout << "You are not eligible for voting.";
            } else {
                cout << "You are eligible for voting.";
            }
    }

    return 0;
}

Output

Enter your age: 21
You are eligible for voting. 

Problem 4:

Write a program to print the month name by given number.

Run
#include<iostream>
using namespace std;
int main() {
    int monthNum;
    cout << "Enter the number of the month (1-12): "; cin >> monthNum;
    switch (monthNum) {
        case 1:
            cout << "January";
            break;
        case 2:
            cout << "February";
            break;
        case 3:
            cout << "March";
            break;
        case 4:
            cout << "April";
            break;
        case 5:
            cout << "May";
            break;
        case 6:
            cout << "June";
            break;
        case 7:
            cout << "July";
            break;
        case 8:
            cout << "August";
            break;
        case 9:
            cout << "September";
            break;
        case 10:
            cout << "October";
            break;
        case 11:
            cout << "November";
            break;
        case 12:
            cout << "December";
            break;
        default:
            cout << "Invalid month number";
    }

    return 0;
}

Output

Enter the number of the month (1-12): 6
June

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription