[Solved] How to calculate circle area by given radius in JavaScript?

  

4
Topic starter

I must write a JavaScript function that calculates circle area by given radius. Print the area as it is calculated and then print it rounded to two decimal places.

  • The input comes as a single number argument - the radius of the circle.
  • The output should be printed to the console on a new line for each result.

Example:

Input: 5

Output:
78.53981633974483
78.54

2 Answers
3

My code is:

function circleArea(radius) {
    let area = Math.PI * (radius * radius);
    console.log(area);
    console.log(Math.round(area*100)/100);
}
 
circleArea(5);
1

Two solutions from me:

function circleA(r) {
    console.log(Math.PI * r * r);
    console.log(Math.round(Math.PI * r * r * 100) / 100);
}
circleA(5);
function cA(rad) {
    let area = Math.PI * rad * rad;
    console.log(area);
 
    let areaRounded = Math.round(area * 100) / 100;
    console.log(areaRounded);
}
 
cA(5);
Share: