Example:

switch(n){
   case 2: printf("two"); break;
   case 3: printf("three"); 
}

Example:

switch (letter){
  case 'r': printf("sometimes"); break;
  case 'a': case 'e': case 'i': case 'o': case 'u': printf("is a vowel"); break; 
  default: printf("is a consonant"); 
}

Example:

switch(grade){ 
  case 1: printf("insufficient"); break;
 
             ...
 
   case 5: printf("excellent"); break; 
   default: printf("error"); 
}

Example:

#include <stdio.h>
main(){
  unsigned k;
  scanf("%u", &k);
  switch(k){
    case 1: printf("x>0, y>0"); break;
    case 2: printf("x<0, y>0"); break;
    case 3: printf("x<0, y<0"); break;
    case 4: printf("x>0, y<0"); break;
    default: printf("Error");
  }
}
5
  1. Write a program that prints the possible values of coordinates x and y depending on the quadrant number entered.
 #include <stdio.h>
main(){
  unsigned k;
  scanf("%u", &k);
  switch(k){
    case 1: printf("x>0, y>0"); break;
    case 2: printf("x<0, y>0"); break;
    case 3: printf("x<0, y<0"); break;
    case 4: printf("x>0, y<0"); break;
    default: printf("Error");
  }
}
5

2. Write a program that reads an operator symbol (+, -, * or /) and two real operands, and then displays the result.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() 
{
    float a, b; char op;
    scanf("%f%f", &a, &b);
    scanf(" %c", &op);
    switch (op) 
    {
    case '+': 
        printf("Sum is %f
", a + b); 
        break;
    case '-': 
        printf("Difference is %f
", a - b); 
        break;
    case '*': 
        printf("Product is %f
", a * b); 
        break;
    case '/': 
        if (b == 0)
            printf("Cannot divide by zero
");
        else
            printf("Quotient is %f
", a / b);
        break;
    default: printf("Unknown operation");
    }
}
2.5

3. Write a program that displays the number of days for an entered month number. If the entered value is 2, ask the user whether the year is a leap year.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
	int monthNum;
	printf("enter the number of the current month
");
	scanf("%d", &monthNum);
	switch (monthNum)
	{
	case 1: 
	case 3: 
	case 5: 
	case 7: 
	case 8: 
	case 10: 
	case 12: 
		printf("31
"); 
		break;
	case 4: 
	case 6: 
	case 9: 
	case 11: 
		printf("30
"); 
		break;
	case 2: 
		printf("Leap year (y/n)?
");
		char leap;
		scanf(" %c", &leap);// space before % prevents scanf from skipping input
		if (leap == 'y' || leap == 'Y' || leap == 'd' || leap == 'D')
			printf("29
");
		else
			printf("28
");
		break;
	default:
		printf("Error!
");
		break;
	}
}
5

Homework: Write a switch statement equivalent to the following:

1
2
3
4
if(k==0) r++;
else if(k==1) s++;
       else if(k==2 || k==3) t++;
 

 

 

1. Write a program that prints the day name for an entered day number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
main(){
  int rbd;
  scanf("%d", &rbd);
  switch (rbd){
    case 1: printf("Mon"); break;
    case 2: printf("Tue"); break;
    case 3: printf("Wed"); break;
    case 4: printf("Thu"); break;
    case 5: printf("Fri"); break;
    case 6: printf("Sat"); break;
    case 7: printf("Sun"); break;
    default: printf("Error");
  }
}

2. Write a program that determines which quadrant contains the triangle formed by the line y=ax+b and the coordinate axes (b≠0).

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
main(){
  float a, b;
  scanf("%f%f", &a, &b);
  if(a>0)
    if(b>0) printf("II");
    else printf("IV");
  else if(b>0)printf("I");
         else printf("III");
}

3. Write a program that displays the area of a shape specified by number: 1 - rectangle, 2 – square, 3 – circle, 4 – triangle, with input validation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Write a program that displays the area of a shape specified by number: 
// 1 - rectangle, 2 – square, 3 – circle, 4 – triangle, with input validation
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void main() 
{
    int choice, err = 0; 
    float a, b, c, r, s;
    printf("1 - rectangle2 - square3 - circle4 - triangle Enter:");
    scanf("%d", &choice);
    switch (choice) 
    {
    case 1: 
        printf("Enter a and b:");
        scanf("%f%f", &a, &b);
        if (a > 0 && b > 0) 
            printf("A=%.2f", a * b);
        else 
            err = 1; 
        break;
    case 2: 
        printf("Enter a:");
        scanf("%f", &a);
        if (a > 0) 
            printf("A=%.2f", pow(a, 2));
        else 
            err = 1; 
        break;
    case 3: 
        printf("Enter r:");
        scanf("%f", &r);
        if (r > 0) 
            printf("A=%.2f", pow(r, 2) * 3.1416);
        else 
            err = 1;
        break;
    case 4: 
        printf("Enter a, b and c:");
        scanf("%f%f%f", &a, &b, &c);
        if (a > 0 && b > 0 && c > 0)
            if (a + b > c && a + c > b && b + c > a) // triangle inequality condition
            {
                s = (a + b + c) / 2;
                printf("A=%.2f", sqrt(s * (s - a) * (s - b) * (s - c)));
            }
            else 
                err = 1;
        else 
            err = 1;
        break;
    default: 
        err = 1;
    }
    if (err == 1) 
        printf("Error");
}