0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Find Area of Circle

Use $area = [math]::PI * $radius * $radius in PowerShell to calculate the area of a circle where $radius is the circle's radius.
📋

Examples

Inputradius = 0
OutputArea of circle with radius 0 is 0
Inputradius = 5
OutputArea of circle with radius 5 is 78.5398163397448
Inputradius = 10.5
OutputArea of circle with radius 10.5 is 346.360590058274
🧠

How to Think About It

To find the area of a circle, you multiply pi by the square of the radius. You get the radius from the user or a variable, then use the formula area = π × radius² to calculate the area.
📐

Algorithm

1
Get the radius value as input
2
Calculate the area by multiplying pi with radius squared
3
Print the calculated area
💻

Code

powershell
$radius = Read-Host 'Enter the radius of the circle'
$radius = [double]$radius
$area = [math]::PI * $radius * $radius
Write-Output "Area of circle with radius $radius is $area"
Output
Enter the radius of the circle: 5 Area of circle with radius 5 is 78.5398163397448
🔍

Dry Run

Let's trace radius = 5 through the code

1

Input radius

$radius = 5

2

Calculate area

$area = 3.14159265358979 * 5 * 5 = 78.5398163397448

3

Output result

Print 'Area of circle with radius 5 is 78.5398163397448'

StepVariableValue
1$radius5
2$area78.5398163397448
3OutputArea of circle with radius 5 is 78.5398163397448
💡

Why This Works

Step 1: Getting radius input

We use Read-Host to ask the user for the radius and convert it to a number with [double].

Step 2: Calculating area

The formula π × radius² is implemented using [math]::PI and multiplying the radius by itself.

Step 3: Displaying result

We print the result with Write-Output showing the radius and calculated area.

🔄

Alternative Approaches

Using a function
powershell
function Get-CircleArea {
  param([double]$radius)
  return [math]::PI * $radius * $radius
}
$radius = 7
$area = Get-CircleArea -radius $radius
Write-Output "Area of circle with radius $radius is $area"
This approach wraps the calculation in a reusable function for cleaner code and easier reuse.
Using inline calculation without variable
powershell
$radius = 3
Write-Output "Area of circle with radius $radius is $([math]::PI * $radius * $radius)"
This is a shorter one-liner that calculates and prints the area directly without storing it in a variable.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of operations (multiplication and accessing pi), so it runs in constant time.

Space Complexity

Only a few variables are used to store input and output, so space usage is constant.

Which Approach is Fastest?

All approaches run in constant time; using a function adds slight overhead but improves code reuse.

ApproachTimeSpaceBest For
Basic scriptO(1)O(1)Simple quick calculation
Function methodO(1)O(1)Reusable code in larger scripts
Inline calculationO(1)O(1)Short scripts or one-liners
💡
Always convert user input to a number type like [double] before calculations to avoid errors.
⚠️
Forgetting to convert the input string to a number causes calculation errors or unexpected results.