How to calculate the perimeter and area of a circle using JavaScript

In this article, we will explore the methods for determining both the perimeter and area of a circle utilizing JavaScript. The mathematical equations essential for computing the perimeter and area of a circle are outlined below:

Area of circle = π r r

Perimeter of circle = 2 π r

Both equations necessitate the use of the value of PI. Therefore, in JavaScript, it is essential to utilize the Math.PI property to obtain the value of PI.

Let’s examine an illustration to determine both the perimeter and the area of a circle.

Example

In this instance, the radius of the circle measures 20cm. To calculate the area and circumference, we need to click the provided HTML button.

Example

<!DOCTYPE html>

<html>



<head>

<title>

Math.PI

</title>



</head>



<body>

<h2>

Welcome to the logic-practice.com

</h2>

<p> Here the radius of circle is 20cm </p>

<p>

Click the following button to get the area and perimeter of circle.

</p>

<p id = "para"></p>

<p id = "para1"></p>

<button onclick = "fun()"> Click me </button>



<script> 

var r = 20;

function fun()

{

document.getElementById('para').innerHTML = 'The area of circle with radius 20cm is: ' + Math.PI * r * r ;

document.getElementById('para1').innerHTML = 'The perimeter of circle with radius 20cm is: ' + 2 * Math.PI * r ;  

}

</script> 



</body>



</html>

Output

Upon executing the aforementioned code and then pressing the specified button, the resulting output will be -

Input Required

This code uses input(). Please provide values below: