Switch Program in C

A simple program for addition, subtraction,  multiplication, and division by switch case.

#include<stdio.h>
#include<conio.h>

void main()
{
int calc,i;
int a,b,c;                        
clrscr();

printf(“enter +,-,*,/ for operation”);
scanf(“%s”,&calc);

switch(calc)
{
case ‘+’:                               // for addition
printf(“enter value of a and b”);
scanf(“%d%d”,&a,&b);
c=a+b;              
printf(“additon=%d”,c);
break;

case ‘-‘:                               //  for subtraction
printf(“enter value of a and b”);
scanf(“%d%d”,&a,&b);
c=a-b;
printf(“subtraction=%d”,c);
break;

case ‘*’:                               // for multiplication
printf(“enter value of a and b”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“multiplication=%d”,c);
break;

case ‘/’:                                 // for division
printf(“enter value of a and b”);    
scanf(“%d%d”,&a,&b);
c=a/b;
printf(“division=%d”,c);
break;

default:
printf(“wrong key pressed”);  // If you pressed any other key apart from +,-,*,/
}

getch();
}

Output:

 

 

Leave a Reply