settingsAccountsettings
Menusettings

Q: How to calculate circle area by given radius in JavaScript?

+4 votes

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

asked in JavaScript category by user nikole
edited by user golearnweb

3 Answers

+2 votes

You can see the formula for calculating the area of a circle here:

the area of a circle image

And my code is:

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

circleArea(5);

 

answered by user mitko
+1 vote

My code:

function circleA(r) {
    console.log(Math.PI * r * r);
    console.log(Math.round(Math.PI * r * r * 100) / 100);
}
circleA(5);

 

answered by user golearnweb
0 votes

My code is:

function cA(rad) {
    let area = Math.PI * rad * rad;
    console.log(area);

    let areaRounded = Math.round(area * 100) / 100;
    console.log(areaRounded);
}

cA(5);

 

answered by user samfred5830
...