Skip to content

Instantly share code, notes, and snippets.

@rk4bir
Created March 17, 2019 08:59
Show Gist options
  • Save rk4bir/06b47308c9e0fe6848784b6e37b2436a to your computer and use it in GitHub Desktop.
Save rk4bir/06b47308c9e0fe6848784b6e37b2436a to your computer and use it in GitHub Desktop.
#include <stdio.h>
void main(){
float x,y;
char ch, escape;
printf("Enter x & y\n");
scanf("%f %f", &x, &y);
/*
Automatically a new line character input is made by the c-program itself.
To prevent the unexpected input messing our program, we placed the
escape char variable to hold that unexpected value.
[ NB: There are several way you can handle this problem. ]
*/
scanf("%c", &escape);
printf("Which operation you want to perform? [+, -, / or *]\n");
scanf("%c", &ch);
switch(ch){
case '+':
printf("%.2f + %.2f = %.2f\n", x, y, x+y);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", x, y, x-y);
break;
case '*':
printf("%.2f x %.2f = %.2f\n", x, y, x*y);
break;
case '/':
printf("%.2f / %.2f = %.2f\n", x, y, x/y);
break;
default:
printf("Invalid operation\n");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment