Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int main()
- {
- char choice=0;
- double a, b, radius, area, perimeter, circumference;
- printf("Enter a 2D shape to compute area and perimeter: a - square, b - rectangle, c - circle\n");
- if(scanf("%c", &choice)!=1)
- printf("Invalid input.");
- else {
- switch (choice)
- {
- case 'a':
- printf("Enter the side of the square:\n");
- scanf("%lf", &a);
- if(a <= 0) {
- printf("Invalid input.");
- return 0; }
- area=a*a;
- perimeter=4*a;
- printf("Square area is:%f\n", area);
- printf("Square perimeter is:%f\n", perimeter);
- break;
- case 'b':
- printf("Enter the sides of the rectangle:\n");
- scanf("%lf %lf", &a, &b);
- if((a <= 0) || (b <= 0)) {
- printf("Invalid input.");
- return 0; }
- area = a*b;
- perimeter = 2*a + 2*b;
- printf("Rectangle area is:%f\n", area);
- printf("Rectangle perimeter is:%f\n", perimeter);
- break;
- case 'c':
- printf("Enter the radius of the circle:\n");
- scanf("%lf", &radius);
- if(radius <= 0) {
- printf("Invalid input.");
- return 0; }
- area = M_PI*radius*radius;
- circumference = 2*M_PI*radius;
- printf("Circle area is:%f\n", area);
- printf("Circumference is:%f\n", circumference);
- break;
- default:
- printf("Invalid input.");
- break;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment