
Eric M. answered 08/30/23
"Full Stack Engineer: Transforming Novices into Future Tech Leaders
#include <stdio.h>
// Function to get name of polygon based on the number of sides
const char* getPolygonName(int sides) {
switch (sides) {
case 3: return "Triangle";
case 4: return "Quadrilateral";
case 5: return "Pentagon";
case 6: return "Hexagon";
case 7: return "Heptagon";
case 8: return "Octagon";
case 9: return "Nonagon";
case 10: return "Decagon";
default: return "Polygon";
}
}
int main() {
int n;
double sideLength, perimeter = 0;
// Ask user for the number of sides
printf("Enter the number of sides of your polygon (>= 3): ");
scanf("%d", &n);
if (n < 3) {
printf("A polygon should have at least 3 sides.\n");
return 1; // exit with an error code
}
// Ask user for the length of each side
for (int i = 0; i < n; i++) {
printf("Enter length of side %d: ", i + 1);
scanf("%lf", &sideLength);
perimeter += sideLength;
}
// Display the name of the polygon and its perimeter
printf("\nYour shape is a %s.\n", getPolygonName(n));
printf("The perimeter of the polygon is: %.2lf\n", perimeter);
return 0;
}