Examples of Switch Case

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.

Examples of Switch Case

Switch Case statement

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<stdio.h>
int main ()
{
  char OPERATOR = '+';
  int a = 20, b = 10;
  switch (OPERATOR)
    {
    case '+':
      printf ("ADDITION %d", a + b);
      break;
    case '-':
      printf ("SUBTRACTION %d", a - b);
      break;
    case '*':
      printf ("MULTIPLICATION %d", a * b);
      break;
    case '/':
      printf ("DIVISION %d", a / b);
      break;
    default:
      printf ("No special cases");
      break;
    }
  printf (" Done with switch case");
  return 0;
}

Output

ADDITION 30 Done with switch case

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<stdio.h>
int main ()
{
  int CGPA;
  printf ("Tell me your CGPA");
  scanf ("%d", &CGPA);
  printf("%d\n",CGPA);
  switch (CGPA)
    {
    case 8:
      printf ("You got 8 CGPA.\n");
      break;
    case 10:
      printf ("You got 10 CGPA.\n");
      break;
    case 7:
      printf ("You got 7 CGPA.\n");
      break;
    default:
      printf ("No special cases.\n");
      break;
    }
  printf ("Done with switch case.\n");
  return 0;
}

Output

Tell me your CGPA
9
No special cases.
Done with switch case.

Problem 3:

Write a program using switch case for printing 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<stdio.h>
int main ()
{
  int Age;
  printf ("Tell me your Age:\n");
  scanf ("%d", &Age);
  printf("%d\n",Age);
  switch (Age)
    {
    case (18):
      printf ("You are not elible for voting.\n");
      break;
    case (20):
      printf ("You are eligible for voting.\n");
      break;
    default:
      printf ("No special cases.\n");
      break;
    }
  printf ("Done with switch case.\n");
  return 0;
}

Output

Tell me your Age:
20
20
You are eligible for voting.
Done with switch case.

Problem 4:

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

Run
#include<stdio.h>
int main ()
{
  int monthname = 12;
  printf ("Hey Prepster enjoy ");
  switch (monthname)
    {
    case 1:
      printf ("January");
      break;
    case 2:
      printf ("February");
      break;
    case 3:
      printf ("March");
      break;
    case 4:
      printf ("April");
      break;
    case 5:
      printf ("May");
      break;
    case 6:
      printf ("June");
      break;
    case 7:
      printf ("July");
      break;
    case 8:
      printf ("August");
      break;
    case 9:
      printf ("september");
      break;
    case 10:
      printf ("October");
      break;
    case 11:
      printf ("November");
      break;
    case 12:
      printf ("December");
      break;
    default:
      printf ("Not valid number");
      break;
    }
  return 0;
}

Output

Hey Prepster enjoy December

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