Calculate difference between circumference and area of a circle - JavaScript


Circumference of a circle is given by −

pi * (r * r)

And area of a circle is given by −

2 * pi * r

Where r is the radius of the circle.

We are required to write a JavaScript function that takes in the radius of circle and calculates the difference between the area and the circumference of the circle

Example

Let’s write the code for this function −

const rad = 6;
const circleDifference = radius => {
   const area = Math.PI * (radius * radius);
   const circumference = 2 * Math.PI * radius;
   const diff = Math.abs(area - circumference);
   return diff;
};
console.log(circleDifference(rad));
console.log(circleDifference(5.25));
console.log(circleDifference(1));

Output

The output in the console −

75.39822368615503
53.60342465187584
3.141592653589793

Updated on: 15-Sep-2020

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements