In this post, we will know about code for c program to create a calculator using a switch statement.
Code for C program to create a calculator using a switch statement
#include <stdio.h>
#include<conio.h>
int main()
{
char ch;
int a,b,result;
printf("Enter the Operands (+, -, *, /):");
scanf("%c", &ch);
printf("Enter two numbers:\n");
scanf("%d %d", &a, &b);
switch (ch)
{
case '+':
result=a+b;
break;
case '-':
result=a-b;
break;
case '*':
result=a*b;
break;
case '/':
result=a/b;
break;
}
printf ("Result=%d", result);
return 0;
}
Output:
Enter the Operands (+, -, *, /): +
Enter two numbers:
5 9
Result=14
Read: C program to check whether a given alphabet is a vowel or consonant