Programming Example
Program to display arithmetic operator using switch case
Program to display arithmetic operator using switch case
/* Program to display arithmetic operator using switch case.
Author: Atnyla Developer */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,n,s,m,su,d;
printf("enter two no's : ");
scanf("%d%d",&a,&b);
printf("enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: \n");
printf(".....................");
scanf("%d",&n);
switch(n)
{
case 1:
s=a+b;
printf("sum=%d \n",s);
break;
case 2:
m=a*b;
printf("multiply=%d \n",m);
break;
case 3:
su=a-b;
printf("subtraction=%d \n",su);
break;
case 4:
d=a/b;
printf("divission=%d \n",d);
break;
default:
printf("wrong input \n");
break;
}
}
<b>Output 1</b>
enter two no's : 2 5
enter 1 for sum
2 for multiply
3for subtraction
4 for division:
.....................1
sum=7
Press any key to continue . . .
<b>Output 2</b>
enter two no's : 2 5
enter 1 for sum
2 for multiply
3for subtraction
4 for division:
.....................2
multiply=10
Press any key to continue . . .
<b>Output 3</b>
enter two no's : 2 5
enter 1 for sum
2 for multiply
3for subtraction
4 for division:
.....................3
subtraction=-3
Press any key to continue . . .
<b>Output 4</b>
enter two no's : 8 2
enter 1 for sum
2 for multiply
3for subtraction
4 for division:
.....................4
divission=4
Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.