-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct_area.c
34 lines (33 loc) · 812 Bytes
/
struct_area.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<stdio.h>
#include<math.h>
// Define a structure to represent different shapes
struct Shape
{
enum type { Circle = 1, Rectangle } t; // Shape type (Circle or Rectangle)
union dimensions {
float length[2]; // Dimensions for Rectangle (length and breadth)
float radius; // Dimension for Circle (radius)
} d;
} s;
int main() {
float area;
// Read the shape type from the user
scanf("%d", &s.t);
if (s.t == 2) {
// Calculate area for Rectangle
scanf("%f", &s.d.length[0]);
scanf("%f", &s.d.length[1]);
area = s.d.length[0] * s.d.length[1];
printf("Area of the rectangle: %.4f units\n", area);
}
else if (s.t == 1) {
// Calculate area for Circle
scanf("%f", &s.d.radius);
area = 3.14 * pow(s.d.radius, 2);
printf("Area of the circle: %.4f units\n", area);
}
else {
printf("Invalid choice!\n");
}
return 0;
}