Guest User

Untitled

a guest
Jan 22nd, 2019
91
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
  12.  
  13. -circle\n");
  14.  
  15.     if(scanf("%c", &choice)!=1)
  16.     printf("Invalid input.");
  17.     else {
  18.  
  19. switch (choice)
  20.    {
  21.        case 'a':
  22.         printf("Enter the side of the square:\n");
  23.         scanf("%lf", &a);
  24.             if(a <= 0) {
  25.                     printf("Invalid input.");
  26.                     return 0; }
  27.  
  28.        area=a*a;
  29.        perimeter=4*a;
  30.  
  31.        printf("Square area is:%f\n", area);
  32.        printf("Square perimeter is:%f\n", perimeter);
  33.        break;
  34.  
  35.         case 'b':
  36.                 printf("Enter the sides of the rectangle:\n");
  37.         scanf("%lf %lf", &a, &b);
  38.                 if((a <= 0) || (b <= 0)) {
  39.                     printf("Invalid input.");
  40.                     return 0; }
  41.  
  42.         area = a*b;
  43.         perimeter = 2*a + 2*b;
  44.  
  45.         printf("Rectangle area is:%f\n", area);
  46.         printf("Rectangle perimeter is:%f\n", perimeter);
  47.         break;
  48.  
  49.         case 'c':
  50.                 printf("Enter the radius of the circle:\n");
  51.         scanf("%lf", &radius);
  52.                 if(radius <= 0) {
  53.                     printf("Invalid input.");
  54.                     return 0; }
  55.  
  56.             area = M_PI*radius*radius;
  57.             circumference = 2*M_PI*radius;
  58.  
  59.             printf("Circle area is:%f\n", area);
  60.             printf("Circumference is:%f\n", circumference);
  61.             break;
  62.  
  63.         default:
  64.                 printf("Invalid input.");
  65.                 break;
  66.    }
  67.    }
  68.    return 0;
  69. }
Add Comment
Please, Sign In to add comment