This blog is under construction

Sunday 7 July 2013

C program to calculate the area and perimeter of a circle, triangle, square and rectangle

C program to calculate the area and perimeter of a circle, triangle, square and rectangle.


  #include <stdio.h>
  #include <math.h>

  int main() {
        float area, perimeter, radius, length, breath, side, triside;

        /* get the inputs from the user */
        printf("Circle:\n");
        printf("Enter the radius of the circle:");
        scanf("%f", &radius);
        printf("\nRectangle: \n");
        printf("Enter the lenght and breath of the rectangle:");
        scanf("%f%f", &length, &breath);
        printf("\nSquare:\n");
        printf("Enter the side of a square:");
        scanf("%f", &side);
        printf("\nEquilateral triangle:\n");
        printf("Enter the side of a triangle:");
        scanf("%f", &triside);
        printf("+------------------------------+\n");
        printf("Geomentry  |  Area  |  Perimeter\n");
        printf("+------------------------------+\n");

        /* area and circumference of the circle */
        area = radius * radius;
        perimeter = 2 * 3.14 * radius;
        printf("Circle     |%8.2f|%5.2f\n", area, perimeter);

        /* area and perimeter of a rectangle */
        area = length * breath;
        perimeter = 2 * (length + breath);
        printf("Rectangle  |%8.2f|%5.2f\n", area, perimeter);

        /* area and perimeter of a square */
        area = side * side;
        perimeter = 4 * side;
        printf("Square     |%8.2f|%5.2f\n", area, perimeter);
        /* area and perimeter of a equilateral triangle */
        area = (sqrt(3) * pow(triside, 2)) / 4;
        perimeter = 3 * triside;
        printf("Triangle   |%8.2f|%5.2f\n", area, perimeter);
        printf("+------------------------------+\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Circle:
  Enter the radius of the circle:10

  Rectangle: 
  Enter the lenght and breath of the rectangle:10 20

  Square:
  Enter the side of a square:10

  Equilateral triangle:
  Enter the side of a triangle:10
  +--------------------------------------+
  Geomentry  |  Area  |  Perimeter
  +--------------------------------------+
  Circle         |  100.00|62.80
  Rectangle   |  200.00 |60.00
  Square       |  100.00 |40.00
  Triangle     |   43.30 |30.00
  +---------------------------------------+





See Also:

No comments:

Post a Comment