Switch Case In C

In this tutorial you will learn about the Switch Case In C programming language through the help of Some examples .




What is Switch Case ??


In  Switch case compiler takes  the value of an variable and test it with Multiple cases and if the match is found it will implement the block of statements that are associated with the particular case is executed .

If the match does not found with any of the above cases then the default statement will be executed .

Why do we need Switch case in C ?


It is similar like the If else ladder but in if else it becomes hectic to read the program whereas in Switch case the program is much more easy and increases readability .

If you want to check the best C and C++ compiler in 2019 then you can refer this " Top Ide for C and C++ Developers - 2019"



Syntax of Switch Case in C :


Here is a basic syntax of  Switch case in C programming Language  :-




switch( expression )
{
 case value-1:
   Block-1;
   Break;
 case value-2:
   Block-2;
   Break;
 case value-n:
   Block-n;
   Break;
 default:
   Block-1;
   Break;
}
Statement-x; 



Switch Case program in C:-


Question-1 Write      program  to  display   the   day     name   using     switch     case 






#include <stdio.h>
#include <conio.h>
int main()
{
int day; 
clrscr();
printf(“enter your choice\n”); 
scanf(“%d”,&day);
switch(day)
{
case 1:
printf(“the day is sunday”); break;
case 2:
printf(“the day is monday”); break;
case 3:
printf(“the day is tuesday”); break;
case 4:
printf(“the day is wednesday”); break;
case 5:
printf(“the day is thursday”); break;
case 6:
printf(“the day is friday”); break;
case 7:
printf(“the day is saturday”); break;
default:
printf(“wrong choice”);
}
return 0;
}






Question-2   Write  a program  to  input   a  character   and  check   whether    it     is   vowel or consonant using switch case ?





#include <stdio.h>
int main()
{
char ch; 
clrscr();
printf(“enter a character\n”); 
scanf(“%c”,&ch);
switch(ch)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
case ‘A’:
case ‘E’:
case ‘I’:
case ‘O’:
case ‘U:
printf(“it is a vowel”); break;
default:
printf(“it is consonant”);
}
return 0;
}







Question-3 Write a program to implement a  simple calculator using Switch case ?





#include <stdio.h>
int main()
{
int a,b,c; 
char ch;
clrscr();
printf(“enter two number\n”); 
scanf(“%d%d”,&a,&b);
 
fflush(stdin);
printf(“enter your choice\n”); 
printf(“1. enter 1 for addition\n”); 
printf(“2. enter 2 for subtraction\n”);
printf(“3. enter 3 for multiplication\n”); 
printf(“4. Enter 4 for division\n”); 
printf(“5. enter 5 for modulo dividion\n”); 
scanf(“%c”,&ch);
switch(ch)
{
case ‘+’:
c=a+b; 
break;
case ‘-‘:
c=a-b; 
break;
case ‘*’:
c=a*b; 
break; 
case ‘/’:
c=a/b; 
break; 
case ‘%’:
c=a%b; 
break; 
default:
printf(“wrong choice”);
}
printf(“calculated value=%d”,c);
return 0;
}




Question-4 Write a program to implement default statement in c using Switch case ?





#include <stdio.h>

int main()
 {
int language = 10;
  switch (language) {
  case 1:
    printf("python\n");
    break;
  case 2:
    printf("C\n");
    break;
  case 3:
    printf("ruby\n");
    break;
  default:
    printf("any language \n");
}
}





If you find anything inappropriate or want to give more knowledge about above topic you can comments us .



Switch Case In C Switch Case In C    Reviewed by Digital Roy on January 16, 2020 Rating: 5

No comments:

Powered by Blogger.