C Program to find the area of a shape

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to calculate the area of a shape?

In this c program, we need to find the area of the shape, which is selected by the user. The logic of this program is first to declare the variable fig_code and displays the menu as:

  • 1 for circle
  • 2 for Rectangle
  • 3 for Triangle
  • 4 for Square.

Then we need to check which shape the user enters and how to use the formula to find the area of that respective shape. So to achieve that, we use a switch statement, and the control of the program passes to the respective case, and we apply that respective formula to find the area of that shape. So if the user enters a circle, control passes to case 1 and uses the formula 3.14* r*r to find the area of a circle, like that if it is a square rectangle or triangle. Finally, in the default case, we will display an error message.

The Formula for finding the Area for the Basic Shapes.

  • Area of Circle = 3.14 * r * r*.
  • Area of Triangle = 0.5 * base * height.
  • Area of Rectangle = breadth * length.
  • Area of Square = side * side.

ALGORITHM

STEP 1: Include the header files to use the built-in function in the C program.

STEP 2: Declare the integer variable fig_code and declare some float variable side, base, length, breadth, height, Area, radius.

STEP 3: Display the menu 1.Circle 2.Rectangle 3.Triangle 4.Square.

STEP 4: Read the figure code into the variable fig_code.

STEP 5: By using a switch statement, check the value of fig_code.

STEP 6: If the fig_code is 1, then read the Radius of the Circle and calculate the Area using the formula 3.142*radius*radius, then Display the Area of the Circle as Area.

STEP 7: If the fig_code is 2, then read the breadth and length of the Rectangle and find out the Area by using the formula Area=breadth * length. Then Display the Area.

STEP 8: If the fig_code is 3, then read the base and height of the Triangle and calculate the Area by using the formula Area=0.5*base * height. Then Display the Area.

STEP 8: If the fig_code is 4, then read the side of the Square and find out the Area by using the formula area=side*side. Then display the Area.

STEP 9: If the fig_code is not equal to 1 or 2 or 3 or 4, we will display the error message.

C Source Code

                                          #include <stdio.h>

void main() {
  int fig_code;
  float side, base, length, breadth, height, area, radius;
  printf("-------------------------\n");
  printf(" 1 --> Circle\n");
  printf(" 2 --> Rectangle\n");
  printf(" 3 --> Triangle\n");
  printf(" 4 --> Square\n");
  printf("-------------------------\n");
  printf("Enter the Figure code\n");
  scanf("%d", & fig_code);
  switch (fig_code) {
  case 1:
    printf("Enter the radius\n");
    scanf("%f", & radius);
    area = 3.142 * radius * radius;
    printf("Area of a circle=%f\n", area); /* case to find the area of circle */
    break;
  case 2:
    printf("Enter the breadth and length\n");
    scanf("%f %f", & breadth, & length);
    area = breadth * length;
    printf("Area of a Rectangle=%f\n", area); /* case to find area of the rectangle */
    break;
  case 3:
    printf("Enter the base and height\n");
    scanf("%f %f", & base, & height);
    area = 0.5 * base * height;
    printf("Area of a Triangle=%f\n", area); /* case to find the area of the triangle  */
    break;
  case 4:
    printf("Enter the side\n");
    scanf("%f", & side);
    area = side * side;
    printf("Area of a Square=%f\n", area); /* case to find area of square */
    break;
  default:
    printf("Error in figure code\n");
    break;
  } /* End of switch */
} /* End of main() */
                                      

OUTPUT

Run 1
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
2
6
Area of a Rectangle=12.000000

Run 2
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
3
 Enter the base and height
5
7
Area of a Triangle=17.500000