C program Lab Material for practice on switch-case
C program Lab Material for practice on switch-case
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int choice; int choice;
printf("Enter a number:"); printf("Enter a number:");
scanf("%d",&choice); scanf("%d",&choice);
switch(choice) switch(choice)
{ {
case 1: case 1:
printf("You typed 1"); case 2:
break; printf("You typed 1 or 2");
case 2: break;
printf("You typed 2"); case 3:
break; case 4:
case 3: printf("You typed 3 or 4");
printf("You typed 3"); break;
break; default:
default: printf("You typed something else");
printf("You typed something else"); }
} return 0;
return 0;
}
}
#include<stdio.h>
int main()
{
char choice;
printf("Enter a character:");
scanf("%c",&choice);
switch(choice)
{
case 'a':
printf("You typed a");
break;
case 'b':
printf("You typed b");
break;
default:
printf("You typed something else");
}
return 0;
}
Nested switch case statement:
#include <stdio.h>
int main ()
{
int a,b;
printf("Enter the value of a and b:");
scanf("%d %d",&a, &b);
switch(a)
{
case 1:
printf("This is part of outer switch\n", a );
switch(b)
{
case 2:
printf("This is part of inner switch\n", a );
break;
default:
printf("Inner switch default value\n");
}
break;
default:
printf("Default value\n");
}
return 0;
}