fork download
  1. #include <stdio.h>
  2.  
  3. // **************************************************
  4. // Function: calcAreaOfTriangle
  5. //
  6. // Description: Calculates the area of a triangle
  7. //
  8. // Parameters: base - base of the triangle
  9. // height - height of the triangle
  10. //
  11. // Returns: area - area of triangle
  12. // ***************************************************
  13. float calcAreaOfTriangle(float base, float height) {
  14. float area; // area of the triangle
  15.  
  16. // calculate area
  17. area = 0.5 * base * height;
  18.  
  19. return area;
  20. } // calcAreaOfTriangle
  21.  
  22. // **************************************************
  23. // Function: calcAreaOfCircle
  24. //
  25. // Description: Calculates the area of a circle
  26. //
  27. // Parameters: radius - radius of the circle
  28. //
  29. // Returns: area - area of circle
  30. // ***************************************************
  31. float calcAreaOfCircle(float radius) {
  32. float area; // area of the circle
  33.  
  34. // calculate area
  35. area = 3.14159 * radius * radius;
  36.  
  37. return area;
  38. } // calcAreaOfCircle
  39.  
  40. // **************************************************
  41. // Function: calcAreaOfSquare
  42. //
  43. // Description: Calculates the area of a square
  44. //
  45. // Parameters: side - side length of the square
  46. //
  47. // Returns: area - area of square
  48. // ***************************************************
  49. float calcAreaOfSquare(float side) {
  50. float area; // area of the square
  51.  
  52. // calculate area
  53. area = side * side;
  54.  
  55. return area;
  56. } // calcAreaOfSquare
  57.  
  58. // **************************************************
  59. // Function: calcAreaOfRectangle
  60. //
  61. // Description: Calculates the area of a rectangle
  62. //
  63. // Parameters: width - width of the rectangle
  64. // height - height of the rectangle
  65. //
  66. // Returns: area - area of rectangle
  67. // ***************************************************
  68. float calcAreaOfRectangle(float width, float height) {
  69. float area; // area of the rectangle
  70.  
  71. // calculate area
  72. area = width * height;
  73.  
  74. return area;
  75. } // calcAreaOfRectangle
  76.  
  77. // **************************************************
  78. // Function: calcAreaOfParallelogram
  79. //
  80. // Description: Calculates the area of a parallelogram
  81. //
  82. // Parameters: base - base of the parallelogram
  83. // height - height of the parallelogram
  84. //
  85. // Returns: area - area of parallelogram
  86. // ***************************************************
  87. float calcAreaOfParallelogram(float base, float height) {
  88. float area; // area of the parallelogram
  89.  
  90. // calculate area
  91. area = base * height;
  92.  
  93. return area;
  94. } // calcAreaOfParallelogram
  95.  
  96. // **************************************************
  97. // Function: calcAreaOfTrapezoid
  98. //
  99. // Description: Calculates the area of a trapezoid
  100. //
  101. // Parameters: a - length of the first parallel side
  102. // b - length of the second parallel side
  103. // height - height of the trapezoid
  104. //
  105. // Returns: area - area of trapezoid
  106. // ***************************************************
  107. float calcAreaOfTrapezoid(float a, float b, float height) {
  108. float area; // area of the trapezoid
  109.  
  110. // calculate area
  111. area = 0.5 * (a + b) * height;
  112.  
  113. return area;
  114. } // calcAreaOfTrapezoid
  115.  
  116. int main() {
  117. // Example usage
  118. printf("Area of Triangle: %f\n", calcAreaOfTriangle(5.0, 10.0));
  119. printf("Area of Circle: %f\n", calcAreaOfCircle(5.0));
  120. printf("Area of Square: %f\n", calcAreaOfSquare(4.0));
  121. printf("Area of Rectangle: %f\n", calcAreaOfRectangle(4.0, 6.0));
  122. printf("Area of Parallelogram: %f\n", calcAreaOfParallelogram(5.0, 10.0));
  123. printf("Area of Trapezoid: %f\n", calcAreaOfTrapezoid(3.0, 4.0, 5.0));
  124.  
  125. return 0;
  126. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
Area of Triangle: 25.000000
Area of Circle: 78.539749
Area of Square: 16.000000
Area of Rectangle: 24.000000
Area of Parallelogram: 50.000000
Area of Trapezoid: 17.500000