Guest User

Untitled

a guest
Jan 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5.  
  6. int main()
  7. {
  8.     char choice=0;
  9.     double a, b, radius, area, perimeter, circumference;
  10.  
  11.     printf("Enter a 2D shape to compute area and perimeter: a - square, b - rectangle, c - circle\n");
  12.  
  13.     if(scanf("%c", &choice)!=1)
  14.     printf("Invalid input.");
  15.     else {
  16.  
  17. switch (choice)
  18.    {
  19.        case 'a':
  20.         printf("Enter the side of the square:\n");
  21.         scanf("%lf", &a);
  22.             if(a <= 0) {
  23.                     printf("Invalid input.");
  24.                     return 0; }
  25.  
  26.        area=a*a;
  27.        perimeter=4*a;
  28.  
  29.        printf("Square area is:%f\n", area);
  30.        printf("Square perimeter is:%f\n", perimeter);
  31.        break;
  32.  
  33.         case 'b':
  34.                 printf("Enter the sides of the rectangle:\n");
  35.         scanf("%lf %lf", &a, &b);
  36.                 if((a <= 0) || (b <= 0)) {
  37.                     printf("Invalid input.");
  38.                     return 0; }
  39.  
  40.         area = a*b;
  41.         perimeter = 2*a + 2*b;
  42.  
  43.         printf("Rectangle area is:%f\n", area);
  44.         printf("Rectangle perimeter is:%f\n", perimeter);
  45.         break;
  46.  
  47.         case 'c':
  48.                 printf("Enter the radius of the circle:\n");
  49.         scanf("%lf", &radius);
  50.                 if(radius <= 0) {
  51.                     printf("Invalid input.");
  52.                     return 0; }
  53.  
  54.             area = M_PI*radius*radius;
  55.             circumference = 2*M_PI*radius;
  56.  
  57.             printf("Circle area is:%f\n", area);
  58.             printf("Circumference is:%f\n", circumference);
  59.             break;
  60.  
  61.         default:
  62.                 printf("Invalid input.");
  63.                 break;
  64.    }
  65.    }
  66.    return 0;
  67.  
  68. }
Add Comment
Please, Sign In to add comment