Orry Kaplan
Orry Kaplan

Reputation: 3

PHP: Area and Circumference solutions

I need some guidance as I have code below i need to solve and I'm not sure if I've done them correctly or not because I feel they are right and I am getting an output but I'm not sure if its the right output.
Basically I need to find the circumference and area, when the given radius is 20.0. I have the need equation at the top of each code snippet. I just want to know if I am right or along the right tracks as I'am new to PHP.

//π΄π‘Ÿπ‘’π‘Ž = πœ‹π‘Ÿ2//

$r = 20.0;
$Area = pi() * (pow($r,2)); //PI = 3.1415926535898
echo $Area, '<br/>';

//πΆπ‘–π‘Ÿπ‘π‘’π‘šπ‘“π‘’π‘Ÿπ‘’π‘›π‘π‘’ = 2πœ‹π‘Ÿ//

$Circumference = 2 * pi() * $r;
echo $Circumference, '<br/>';

Upvotes: 0

Views: 146

Answers (2)

kispi
kispi

Reputation: 185

Seems fine. Also, put 4 spaces for each code so that they can be showed to others as code snippet. Otherwise, your code will not likely to be easy-readable.

Upvotes: 0

Anis Alibegić
Anis Alibegić

Reputation: 3230

You code is working properly.

Area

//π΄π‘Ÿπ‘’π‘Ž = πœ‹π‘Ÿ2//
$r = 20.0;
$Area = pi() * (pow($r,2)); //PI = 3.1415926535898
echo $Area, '<br/>';

Your output: 1256.6370614359

Google output:

Google screenshot


Circumference

//πΆπ‘–π‘Ÿπ‘π‘’π‘šπ‘“π‘’π‘Ÿπ‘’π‘›π‘π‘’ = 2πœ‹π‘Ÿ//
$Circumference = 2 * pi() * $r;
echo $Circumference, '<br/>';

Your output: 125.66370614359

Google output:

Google screenshot


You could make it a little bit better by adding functions:

function calculateCircleArea($r) {
    return pi() * (pow($r,2));
}

function calculateCircleCircumference($r) {
    return 2 * pi() * $r;
}

echo calculateCircleArea(20.0).'<br/>';
echo calculateCircleCircumference(20.0);

Upvotes: 1

Related Questions