Thursday 1 February 2018

Example Switch Statement in C Language

Write a program to calculate the addition, subtraction, multiplication and division of two integer number using switch statement.

#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
float a,b,c;
int n;
printf(“enter the value of a,b and n”);
scanf(“%f%f%d”,&a,&b,&n);
switch(n)
{
case 1:
c=a+b;
break;
case 2:
c+a-b;
break;
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
defult:
printf(“value is not valid”);
}
Printf(“\n c=%f”,c);
getch( );
}


Output:

enter the value of a,b and n 2 4 1
c=6.00

enter the value of a,b and n 2 4 2
c=-2.00

enter the value of a,b and n 2 4 3
c=8.00

enter the value of a,b and n 2 4 4
c=2.00

enter the value of a,b and n 2 4 6

value is not valid

Share this

0 Comment to "Example Switch Statement in C Language"

Post a Comment