Area of Shapes - Problem

You are given three different shapes and need to calculate their areas based on the provided dimensions:

  • Circle: Given radius r, calculate area using formula π × r²
  • Rectangle: Given length l and width w, calculate area using formula l × w
  • Triangle: Given base b and height h, calculate area using formula ½ × b × h

The program should read the shape type and its dimensions, then output the calculated area rounded to 2 decimal places.

Note: Use π = 3.14159 for circle calculations.

Input & Output

Example 1 — Circle Area
$ Input: shape = "circle", dimensions = [5]
Output: 78.54
💡 Note: Circle area = π × r² = 3.14159 × 5² = 3.14159 × 25 = 78.54 square units
Example 2 — Rectangle Area
$ Input: shape = "rectangle", dimensions = [6, 4]
Output: 24.00
💡 Note: Rectangle area = length × width = 6 × 4 = 24.00 square units
Example 3 — Triangle Area
$ Input: shape = "triangle", dimensions = [8, 3]
Output: 12.00
💡 Note: Triangle area = ½ × base × height = 0.5 × 8 × 3 = 12.00 square units

Constraints

  • Shape will be one of: "circle", "rectangle", "triangle"
  • Dimensions array will have appropriate length for each shape
  • All dimension values will be positive numbers
  • Results should be rounded to 2 decimal places

Visualization

Tap to expand
INPUTShape: circleDimensions: [5]radius5Circle with radius 5ALGORITHM1Identify shape type2Apply formula: π × r²3Calculate: 3.14159 × 254Round to 2 decimalsCalculation78.5398...RESULTArea78.54sq unitsFinal calculated arearounded to 2 decimalsKey Insight:Mathematical formulas provide instant area calculations from basic measurements,eliminating the need to count individual units or estimate curved boundaries.TutorialsPoint - Area of Shapes | Direct Formula Application
Asked in
Google 15 Amazon 12 Microsoft 10
12.0K Views
Medium Frequency
~8 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen