JavaScript program to find area of a circle


We will be using the below formula to find the area of a circle −

pi * r^2

In the program, we will prompt the user to input the radius of the circle. Then, we will use the formula to calculate the area and print it on the screen.

Approach

  • Get the radius of the circle from the user as an input.

  • Calculate the area using the formula: area = π * radius^2

  • Store the value of π, it can be calculated using Math.PI in JavaScript.

  • Multiply the value of π with the square of the radius to get the area.

  • Round off the result to required decimal places, if necessary, using the toFixed() method.

  • Return or print the calculated area.

Example

Here is an example of a JavaScript program that calculates the area of a circle

const radius = 10;
const pi = Math.PI;
const areaOfCircle = (radius, pi) => {
   return pi * radius * radius;
};
console.log("The area of the circle is: " + areaOfCircle(radius, pi));

Explanation

  • First, we declare two constants: radius and pi. The radius constant is assigned the value 10, and pi is assigned the value of Math.PI which is a built-in constant in JavaScript that represents the value of pi (3.14159...).

  • Next, we define a function named areaOfCircle that takes two parameters radius and pi.

  • Inside the function, we use the formula for the area of a circle, which is pi * radius * radius, and return the result.

  • Finally, we use console.log() to display the result of calling areaOfCircle with radius and pi as arguments. The result is a string that says "The area of the circle is: [area]".This program calculates the area of a circle with a radius of 10 and outputs the result to the console.

Updated on: 13-Mar-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements