Advertisement
18126

Day1(ex6)

Jul 7th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int main()
  6. {
  7.     char operation;
  8.     printf("Enter operation: (+ , -, *, /, ^)");
  9.     scanf("%c", &operation);
  10.     float n1, n2;
  11.     printf("Enter number 1: ");
  12.     scanf("%f", &n1);
  13.     printf("Enter number 2: ");
  14.     scanf("%f", &n2);
  15.     if(operation == '+') {
  16.         printf("%.2f + %.2f = %.2f", n1, n2, (n1+n2));
  17.     }
  18.     else if(operation == '-') {
  19.         printf("%.2f - %.2f = %.2f", n1, n2, (n1-n2));
  20.     }
  21.     else if(operation == '*') {
  22.         printf("%.2f * %.2f = %.2f", n1, n2, (n1*n2));
  23.     }
  24.     else if(operation == '/') {
  25.         if(n2 == 0) {
  26.             printf("Can't divide by 0.");
  27.             return -1;
  28.         }
  29.         printf("%.2f / %.2f = %.2f", n1, n2, (n1/n2));
  30.     }
  31.     else if(operation == '^') {
  32.         printf("%.2f^%.2f = %.2f", n1, n2, (powf(n1, n2)));
  33.     }
  34.     else {
  35.         printf("Operation is not recognized.");
  36.         return -1;
  37.     }
  38.     return 0;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement