diff --git a/PracticeProblems/README.md b/PracticeProblems/README.md index 3916d10..a1402d2 100644 --- a/PracticeProblems/README.md +++ b/PracticeProblems/README.md @@ -7,6 +7,37 @@ Feel free to submit your solutions and comments! --- 1. Write a program to given two rectangles find print out if they intersect or not. +2. Write a c program for simple calculator using switch Statement. ---- \ No newline at end of file +--- +Solution-2 +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +}