Tutorialspoint
Problem
Solution
Submissions

Calculate the Area of a Circle

Certification: Basic Level Accuracy: 66.67% Submissions: 6 Points: 5

Write a C# program to calculate the area of a circle given its radius. The formula for calculating the area of a circle is A = π × r², where π (pi) is approximately equal to 3.14159 and r is the radius of the circle.

Implement the CalculateArea(double radius) function, which:

  1. Takes a double value representing the radius of the circle
  2. Returns a double value representing the area of the circle
  3. Handles edge cases appropriately (negative radius should return 0)
Example 1
  • Input: radius = 5.0
  • Output: 78.54
  • Explanation:
    • Area = π × 5² = 3.14159 × 25 = 78.54 (rounded to 2 decimal places)
Example 2
  • Input: radius = 10.0
  • Output: 314.16
  • Explanation:
    • Area = π × 10² = 3.14159 × 100 = 314.16 (rounded to 2 decimal places)
Constraints
  • -1000.0 <= radius <= 1000.0
  • Return 0 if radius is negative
  • Time Complexity: O(1)
  • Space Complexity: O(1)
NumberVariables and Data TypesAdobeSwiggy
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use Math.PI for the value of π (3.14159...)
  • Square the radius using Math.Pow(radius, 2) or simply radius * radius
  • Check if radius is negative and return 0 if it is
  • Use proper double precision to maintain accuracy

Steps to solve by this approach:

 Step 1: Check if the radius is negative; if yes, return 0
 Step 2: Use the formula A = pie × r² to calculate the area
 Step 3: Apply Math.PI for the value of pie
 Step 4: Multiply PI by radius squared (either using Math.Pow or radius * radius)
 Step 5: Return the calculated area

Submitted Code :