0

# make calculator that exit if user entering letter in C languange

I want to make a simple calculator using switch cases and functions. The operators used are addition, subtraction, multiplication, and division. Each case will execute its own function from the arithmetic operation. The program will be completed if the input provided is not a number (it can be letters or other characters). how to make it?

8th Apr 2020, 5:15 PM
+ 5
You better show us your trial
8th Apr 2020, 5:19 PM
M Tamim
0
/* C Program to Create Simple Calculator using Switch Case */ #include <stdio.h> float add(float a, float b); float sub(float a, float b); float multi(float a, float b); float div(float a, float b); int main() { char Operator; float num1, num2, result = 0; menu: printf ("Enter num 1 = "); scanf ("%f%c",&num1); printf ("Enter Operator = "); scanf (" %c",&Operator); printf ("Enter num 2 = "); scanf ("%f",&num2); switch(Operator) { case '+': result = add(num1, num2); break; case '-': result = sub(num1, num2); break; case '*': result = multi(num1, num2); break; case '/': result = div(num1, num2); break; default: printf("\n You have enetered an Invalid Operator "); break; } printf("\n The result of %.2f %c %.2f = %.2f\n\n", num1, Operator, num2, result); goto menu; return 0; } float add(float a, float b) { return a + b; } float sub(float a, float b) { return a - b; } float multi(float a, float b) { return a * b; } float div(float a, float b) { return a / b; }
8th Apr 2020, 5:52 PM