
How to calculate the perimeter and area of a circle using JavaScript
To calculate the perimeter and area of a circle in JavaScript, you need to use the mathematical formulas associated with circles. The formulas are as follows:
Perimeter (also known as circumference) = 2 * π * radius
Area = π * radius^2
Here’s an example of how you can calculate the perimeter and area of a circle using JavaScript:
// Define the radius of the circle
var radius = 5;
// Calculate the perimeter (circumference)
var perimeter = 2 * Math.PI * radius;
// Calculate the area
var area = Math.PI * Math.pow(radius, 2);
// Display the results
console.log("Perimeter: " + perimeter);
console.log("Area: " + area);
In this example, the radius of the circle is set to 5
. You can change the value of radius
to any desired value.
To calculate the perimeter, the formula 2 * Math.PI * radius
is used, where Math.PI
represents the mathematical constant π. The result is stored in the perimeter
variable.
To calculate the area, the formula Math.PI * Math.pow(radius, 2)
is used. The Math.pow()
function is used to calculate the square of the radius. The result is stored in the area
variable.
Finally, the results are displayed in the console using console.log()
.
By executing this code, you will obtain the calculated perimeter (circumference) and area of the circle based on the given radius.