Switch case in c programming with source code
Switch case in c:
A switch statement test the value of a variable and compare it with multiple case once the case match is found a block of statement associated with that particular case is executed. Each case in a block of a switch has a different name/ number which is referred to as an identifier . The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached the switch terminate.
Syntax :
Switch (statement)
{
case 1: statement 1;
Break;
case 2: statement 2;
Break;
case 3: statement 3;
Break;
.......................................
........................................
.......................................
.......................................
case n: statement n;
Break;
Default : Statement n+1;
}
1. WAP to print result according to use/switch case week days.
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
printf("Enter the case number x:");
Scanf("%d",&x);
switch(x)
{
case 1: printf("monday");
break;
case 2: printf("tuesday");
break;
case 3: printf("wednesday");
break;
case 4: printf("thursday");
break;
case 5: printf("friday");
break;
case 6: printf("saturday");
break;
case 7: printf("sunday");
break;
default : printf("Enter the case number between 1 to 7
only :");
}
getch();
}
Output:
Enter the case number x : 2
tuesday
Enter the case number x: 5
friday
2. WAP to print result according to use switch case for print calculator operation.
#include<stdio.h>
#include<coio.h>
void main()
{
int a,b,c;
char x;
printf("Enter the value of a and b :");
scanf("%d %d", &a, &b);
printf(" Enter the case x :");
scanf("%d",&x);
switch(x)
{
case '+' : c=a+b;
printf("%d",c);
break;
case '-' : c=a-b ;
printf("%d",c);
break;
case '/' : c=a/b;
printf("%d",c);
break;
case '*' : c=a*b;
printf("%d",c);
break;
case '%' : c=a%b;
printf("%d",c);
break;
default : printf("Enter the case with operator
+,-,/,*,% only);
}
getch();
}
Switch case in c programming with source code
Reviewed by For Learnig
on
February 24, 2023
Rating:
No comments:
If you have any doubts, please tell me know