Thursday, 28 August 2014

Example For Switch Case

Q:- Write a program in C to show the use of Switch Case.

In this program we will ask user two numbers and then do different Arithmetic Operations on it using switch case.

SWITCH CASE DEFINED
#include <conio.h>
#include <stdio.h>
void main()
{
float a,b,d,c;
char ch;
clrscr ();
printf("\nEnter any two numbers\n");
scanf("%d%d",&a,&b);
do
{
printf("\nEnter 1 for add, 2 for sub, 3for mul, 4 for div\n");
flushall ( );
scanf("%d",&d);
switch(d)
{
case 1:
d=a+b;
printf("\n sum of %d and %d is %d",a,b,d);
break;
case 2:
d=a-b;
printf("\nsub of %d and %d is %d",a,b,d);
break;
case 3:
d=a*b;
printf("\nmul of %d and %d is %d",a,b,d);
break;
case 4:
d=a/b;
printf("\ndiv of %d and %d is %d",a,b,d);
break;
default:
printf("\ninvalid choice\n");
}
printf("\nPress y to continue\n");
ch=getche();
}
while((ch=='y')||(ch=='Y'));
getch();
}


No comments:

Post a Comment

Tell Us What You've Got...