Write a program to find the area of rectangle, square and circle.

  1. /*
  2.  * C program to find the areas of different geometrical shapes such as
  3.  * circle, square, rectangle etc using switch statements.
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     int fig_code;
  10.     float side, base, length, breadth, height, area, radius;
  11.  
  12.     printf("-------------------------\n");
  13.     printf(" 1 --> Circle\n");
  14.     printf(" 2 --> Rectangle\n");
  15.     printf(" 3 --> Triangle\n");
  16.     printf(" 4 --> Square\n");
  17.     printf("-------------------------\n");
  18.     printf("Enter the Figure code\n");
  19.     scanf("%d", &fig_code);
  20.     switch(fig_code)
  21.     {
  22.     case 1:
  23.         printf("Enter the radius\n");
  24.         scanf("%f", &radius);
  25.         area = 3.142 * radius * radius;
  26.         printf("Area of a circle = %f\n", area);
  27.         break;
  28.     case 2:
  29.         printf("Enter the breadth and length\n");
  30.         scanf("%f %f", &breadth, &length);
  31.         area = breadth * length;
  32.         printf("Area of a Reactangle = %f\n", area);
  33.         break;
  34.     case 3:
  35.         printf("Enter the base and height\n");
  36.         scanf("%f %f", &base, &height);
  37.         area = 0.5 * base * height;
  38.         printf("Area of a Triangle = %f\n", area);
  39.         break;
  40.     case 4:
  41.         printf("Enter the side\n");
  42.         scanf("%f", &side);
  43.         area = side * side;
  44.         printf("Area of a Square=%f\n", area);
  45.         break;
  46.     default:
  47.         printf("Error in figure code\n");
  48.         break;
  49.     }
  50. }
$ cc pgm77.c
$ a.out
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
1
Enter the radius
30
Area of a circle = 2827.800049
 
$ a.out
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
20 30
Area of a Reactangle = 600.000000
 
$ a.out
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
3
Enter the base and height
45 80
Area of a Triangle = 1800.000000
 
$ a.out
-------------------------
 1 --> Circle
 2 --> Rectangle
 3 --> Triangle
 4 --> Square
-------------------------
Enter the Figure code
4
Enter the side
100
Area of a Square=10000.000000