Tuesday, April 14, 2020

Switch statement in C

  • Flowchart :

  • Syntax :
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;
  • Program :
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,g,operator;
clrscr();
printf("\n----------------SWITCH STATEMENT----------------");
printf("\n The operators are  \n1.Add  \n2.Subtract  \n3.multiply  \n4.divide  \n5.modulo");
printf("\n Enter the operator:");
scanf("%d",&operator);
printf("\n Enter the Two numbers:");
scanf("%d%d",&a,&b);
switch(operator)
{
case 1:
c=a+b;
printf("\nsum is : %d",c);
break;
case 2:
d=a-b;
printf("\ndifference is : %d",d);
break;
case 3:
e=a*b;
printf("\nmultiply is : %d",e);
break;
case 4:
f=a/b;
printf("\ndivide is : %d",f);
break;
case 5:
g=a%b;
printf("\nmodulo is : %d",g);
break;
default:
printf("\n Error");
break;
}
getch();
}

  • Output :




No comments:

Post a Comment

Function Overloading in C++

https://monitechvenkat1620.blogspot.com Program : #include <iostream> using namespace std; int c(int x, int y) {   return...