From b629e426451073c8a489ceb5b15c67714f7fce7a Mon Sep 17 00:00:00 2001 From: Ankit-07-17 <72241661+Ankit-07-17@users.noreply.github.com> Date: Fri, 2 Oct 2020 10:21:57 +0530 Subject: [PATCH] switch_case.c This is an example of switch case. --- switch_case.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 switch_case.c diff --git a/switch_case.c b/switch_case.c new file mode 100644 index 0000000..b248bd7 --- /dev/null +++ b/switch_case.c @@ -0,0 +1,37 @@ +// Program to create a simple calculator +#include + +int main() { + char operator; + double n1, n2; + + printf("Enter an operator (+, -, *, /): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf",&n1, &n2); + + switch(operator) + { + case '+': + printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2); + break; + + case '-': + printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2); + break; + + case '*': + printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2); + break; + + case '/': + printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2); + break; + + // operator doesn't match any case constant +, -, *, / + default: + printf("Error! operator is not correct"); + } + + return 0; +}