LATEST

Switch...case statement in C / C++ with example

In this tutorial, we will learn about the switch...case statement in C / C++ and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

Top 10 switch...case in C / C++ with examples

Here, we will implement examples using switch...case in C / C++ from basic to advanced. For each example, we will implement with two different languages C and C++.

Example 1: Create a Calculator using the switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  char oper;
    float num1, num2;
    cout << "Enter an operator (+, -, *, /): ";
    cin >> oper;
    cout << "Enter two numbers: " << endl;
    cin >> num1 >> num2;

    switch (oper) {
        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:
            // operator is doesn't match any case constant (+, -, *, /)
            cout << "Error! The operator is not correct";
            break;
    }

  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  char oper;
    float num1, num2;
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &oper);
    printf("Enter two numbers: \n");
    scanf("%f%f",&num1,&num2);

    switch (oper) {
        case '+':
            printf("%f + %f = %f",num1,num2,num1+num2);
            break;
        case '-':
            printf("%f - %f = %f",num1,num2,num1-num2);
            break;
        case '*':
            printf("%f * %f = %f",num1,num2,num1*num2);
            break;
        case '/':
            printf("%f / %f = %f",num1,num2,num1/num2);
            break;
        default:
            // operator is doesn't match any case constant (+, -, *, /)
            printf("Error! The operator is not correct");
            break;
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 01 PNG

Example 2: Check if the key pressed down is a number with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  char ch;
  cout<<"Enter any key: ";
  cin>>ch;
  switch(ch){
    case '0':
      cout<<"This is number 0.";
      break;
    case '1':
      cout<<"This is number 1.";
      break;
    case '2':
      cout<<"This is number 2.";
      break;
    case '3':
      cout<<"This is number 3.";
      break;
    case '4':
      cout<<"This is number 4.";
      break;
    case '5':
      cout<<"This is number 5.";
      break;
    case '6':
      cout<<"This is number 6.";
      break;
    case '7':
      cout<<"This is number 7.";
      break;
    case '8':
      cout<<"This is number 8.";
      break;
    case '9':
      cout<<"This is number 9.";
      break;
    default:
      cout<<"This is not a number.";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  char ch;
  printf("Enter any key: ");
  scanf("%c", &ch);
  switch(ch){
    case '0':
      printf("This is number 0.");
      break;
    case '1':
      printf("This is number 1.");
      break;
    case '2':
      printf("This is number 2.");
      break;
    case '3':
      printf("This is number 3.");
      break;
    case '4':
      printf("This is number 4.");
      break;
    case '5':
      printf("This is number 5.");
      break;
    case '6':
      printf("This is number 6.");
      break;
    case '7':
      printf("This is number 7.");
      break;
    case '8':
      printf("This is number 8.");
      break;
    case '9':
      printf("This is number 9.");
      break;
    default:
      printf("This is not a number.");
  }
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 02 PNG

Example 3: Program to answer multiple-choice questions with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  char ch;
  cout<<"Question: 10 / 2 = ?\n";
  cout<<"a. 5\n";
  cout<<"a. 6\n";
  cout<<"a. 7\n";
  cout<<"a. 8\n";
  cout<<"Enter your answer: ";
  cin>>ch;
  switch(ch){
    case 'a':
      cout<<"Congratulations you made the right choice!";
      break;
    case 'b':
      cout<<"Sorry, you gave the wrong answer!";
      break;
    case 'c':
      cout<<"Sorry, you gave the wrong answer!";
      break;
    case 'd':
      cout<<"Sorry, you gave the wrong answer!";
      break;
    default:
      cout<<"The answer is not appropriate.";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  char ch;
  printf("Question: 10 / 2 = ?\n");
  printf("a. 5\n");
  printf("a. 6\n");
  printf("a. 7\n");
  printf("a. 8\n");
  printf("Enter your answer: ");
  scanf("%c", &ch);
  switch(ch){
    case 'a':
      printf("Congratulations you made the right choice!");
      break;
    case 'b':
      printf("Sorry, you gave the wrong answer!");
      break;
    case 'c':
      printf("Sorry, you gave the wrong answer!");
      break;
    case 'd':
      printf("Sorry, you gave the wrong answer!");
      break;
    default:
      printf("The answer is not appropriate.");
  }
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 03 PNG

Example 4: Check if a number is divisible by 3 with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int mod, n;
  cout<<"Enter a number: ";
  cin>>n;
  mod = n % 3;
  switch(mod){
    case 0:
      cout<<n<<" is a number divisible by 3";
      break;
    default:
      cout<<n<<" is not a number divisible by 3";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int mod, n;
  printf("Enter a number: ");
  scanf("%d", &n);
  mod = n % 3;
  switch(mod){
    case 0:
      printf("%d is a number divisible by 3", n);
      break;
    default:
      printf("%d is not a number divisible by 3",n);
  }
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 04 PNG

Example 5: The program prints out the days of the week with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int number;
    int n;
    cout<<"Enter number n: ";
    cin>>n;
    switch(n){
      case 2:
        cout<<"This is Monday!";
        break;
      case 3:
        cout<<"This is Tuesday!";
        break;
      case 4:
        cout<<"This is Wednesday!";
        break;
      case 5:
        cout<<"This is Thursday!";
        break;
      case 6:
        cout<<"This is Friday!";
        break;
      case 7:
        cout<<"This is Saturday!";
        break;
      case 8:
        cout<<"This is Sunday!";
        break;
      default:
        cout<<"It's not exist!";
    }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  int number;
    int n;
    printf("Enter number n: ");
    scanf("%d", &n);
    switch(n){
      case 2:
        printf("This is Monday!");
        break;
      case 3:
        printf("This is Tuesday!");
        break;
      case 4:
        printf("This is Wednesday!");
        break;
      case 5:
        printf("This is Thursday!";
        break;
      case 6:
        printf("This is Friday!");
        break;
      case 7:
        printf("This is Saturday!");
        break;
      case 8:
        printf("This is Sunday!");
        break;
      default:
        printf("It's not exist!");
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 05 PNG

Example 6: Program for grading student learning scores 'A', 'B','C' with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int number;
    char ch;
    cout<<"Enter your score: ";
    cin>>ch;
    switch(ch){
      case 'A':
        cout<<"Excellent!";
        break;
      case 'B':
        cout<<"Good!";
        break;
      case 'C':
        cout<<"Medium!";
        break;
      default:
        cout<<"Invalid score!";
    }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  int number;
    char ch;
    printf("Enter your score: ");
    scanf("%c", &ch);
    switch(ch){
      case 'A':
        printf("Excellent!");
        break;
      case 'B':
        printf("Good!");
        break;
      case 'C':
        printf("Medium!");
        break;
      default:
        printf("Invalid score!");
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 06 PNG

Example 7: Check even and odd numbers with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int number, result;
    cout<<"Enter a number: ";
    cin>>number;
    result = number % 2;
    switch(result){
      case 0:
        cout<<"This is a even number.";
        break;
      case 1:
        cout<<"This is a odd number.";
        break;
    }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  int number, result;
    printf("Enter a number: ");
    scanf("%d", &number);
    result = number % 2;
    switch(result){
      case 0:
        printf("This is a even number.");
        break;
      case 1:
        printf("This is a odd number.");
        break;
    }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 07 PNG

Example 8: Create menus with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int number;
  cout<<"-------------Menu-------------\n\n";
  cout<<"\t1. Option 1.\n";
  cout<<"\t2. Option 2.\n";
  cout<<"\t3. Option 3.\n";
  cout<<"\t4. Option 4.\n";
  cout<<"\t5. Option 5.\n";
  cout<<"\t6. Option 6.\n";
  cout<<"\t-> Enter 0 to exit.\n\n";
  cout<<"Enter your choose: ";
  cin>>number;
  switch(number){
    case 1:
      cout<<"You have selected option 1.";
      break;
    case 2:
      cout<<"You have selected option 2.";
      break;
    case 3:
      cout<<"You have selected option 3.";
      break;
    case 4:
      cout<<"You have selected option 4.";
      break;
    case 5:
      cout<<"You have selected option 5.";
      break;
    case 6:
      cout<<"You have selected option 6.";
      break;
    case 0:
      cout<<"Goood byeeee !!!";
      break;
    default:
      cout<<"Your selection is not valid..";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>
int main(void) {
  int number;
  printf("-------------Menu-------------\n\n");
  printf("\t1. Option 1.\n");
  printf("\t2. Option 2.\n");
  printf("\t3. Option 3.\n");
  printf("\t4. Option 4.\n");
  printf("\t5. Option 5.\n");
  printf("\t6. Option 6.\n");
  printf("\t-> Enter 0 to exit.\n\n");
  printf("Enter your choose: ");
  scanf("%d", &number);
  switch(number){
    case 1:
      printf("You have selected option 1.");
      break;
    case 2:
      printf("You have selected option 2.");
      break;
    case 3:
      printf("You have selected option 3.");
      break;
    case 4:
      printf("You have selected option 4.");
      break;
    case 5:
      printf("You have selected option 5.");
      break;
    case 6:
      printf("You have selected option 6.");
      break;
    case 0:
      printf("Goood byeeee !!!");
      break;
    default:
      printf("Your selection is not valid..");
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 08 PNG

Example 9: Check Primes with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main() {
  int number;
  cout<<"Enter a number: ";
  cin>>number;
  int a = isPrimes(number);
  switch(a){
    case 1:
      cout<<number<<" is Primes.";
      break;
    case 0:
      cout<<number<<" is not Primes.";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

bool isPrimes(int n)
{
    if (n < 2){
        return false;
    }       
    if (n == 2){
        return true;
    }
    if (n % 2 == 0){
        return false;   
    }
    for (int i = 3; i < (n - 1); i += 2){
        if (n % i == 0){
            return false;
        }   
    }
    return true;
}

int main(void) {
  int number;
  printf("Enter a number: ");
  scanf("%d", &number);
  int a = isPrimes(number);
  switch(a){
    case 1:
      printf("%d is Primes.",number);
      break;
    case 0:
      printf("%d is not Primes.",number);
      break;
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 09 PNG

Example 10: Check Perfect Square number with switch...case statement in C / C++

C++ programming:

#include <iostream>
using namespace std;

bool isPerfecrSquareNum(int n){
  int i = 0;
  while(i*i <= n){
        if(i*i == n){
            return true;
        }
        ++i;
    }
    return false;
}

int main() {
  int number;
  cout<<"Enter a number: ";
  cin>>number;
  int a = isPerfecrSquareNum(number);
  switch(a){
    case 1:
      cout<<number<<" is Perfect Squares number.";
      break;
    case 0:
      cout<<number<<" is not Perfect Squares number.";
  }
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>
#include <stdbool.h>

bool isPerfecrSquareNum(int n){
  int i = 0;
  while(i*i <= n){
        if(i*i == n){
            return true;
        }
        ++i;
    }
    return false;
}

int main(void) {
  int number;
  printf("Enter a number: ");
  scanf("%d", &number);
  int a = isPerfecrSquareNum(number);
  switch(a){
    case 1:
      printf("%d is Perfect Squares number.",number);
      break;
    case 0:
      printf("%d is not Perfect Squares number.",number);
      break;
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

exam 10 PNG

Cùng chuyên mục:

Strings in C/C++ with examples

Strings in C/C++ with examples

Arrays in C / C++ with examples

Arrays in C / C++ with examples

Multiple conditions for if statement in C / C++

Multiple conditions for if statement in C / C++

Can if statement have 2 conditions? Can you have 3 conditions in an if statement?…

if - else statement in C / C++ with example

if - else statement in C / C++ with example

Do while loop in C / C++ with examples

Do while loop in C / C++ with examples

While loop in C / C++ with examples

While loop in C / C++ with examples

For loop in C / C++ with examples

For loop in C / C++ with examples

Top