Open In App

JavaScript program to find area of a circle

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Learn how to calculate the area of a circle using JavaScript with this easy guide. We will show you a simple JavaScript program that does the math for you. Whether you’re new to coding or looking for a quick refresher, this article has got you covered.

Given the radius of a circle, find the area of that circle. The area of a circle can simply be evaluated using the following formula:

Area of circle

Where r is the radius of the circle and it may be in float because the value of the pie is 3.14

Approach: Using the given radius, find the area using the above formula: (pi * r * r) and print the result in float.

Example: Below is the example that will illustrate the program to find area of a circle:

JavaScript
let pi = 3.14159265358979323846;

// Function to calculate the area of circle 
function findArea(r) {
  return (pi * r * r);
}

// Driver code 
let r, Area;
r = 5;

// Function calling 
Area = findArea(r);

// displaying the area 
console.log("Area of Circle is: " + Area);

Output:

Area of Circle is: 78.53981633974483

Time Complexity: O(1).
Auxiliary Space: O(1), since no extra space has been taken.

Another Approach: Using Math.Pi

We can get the value of pi using the Math module in javascript

Below is the Implementation:

JavaScript
function findArea(r) {
  let pie_value = Math.PI;
  return (pie_value * r * r);
}

// Driver code 
let r, Area;
r = 5;

// Function calling 
Area = findArea(r);

// displaying the area 
console.log("Area of Circle is: " + Area);

Output:

Area of Circle is: 78.53981633974483


Next Article

Similar Reads