In this example, you will learn how to calculate the area and circumference of a circle using a simple JavaScript program.
// Circle radius let radius = 5; // Calc the Area let area = Math.PI*radius*radius; // Calc the circumference let circumference = 2*Math.PI*radius; // Print the area console.log('The area of circle is: ' + area); // Print the circumference console.log('The circumference of circle is: '+ circumference);
Output:
The area of circle is: 78.53981633974483 The circumference of circle is: 31.41592653589793
Example 2:
In the above example, we imported the value of pi(π) from the Math
library of JavaScript. You could also set its value manually and calculate the area and circumference of the circle.
However, this approach will give you the approximate value of the area and circumference, not the exact value:
// Circle radius let radius = 5; // Set PI(π) Value const PI = 3.14 // Calc the Area let area = PI*radius*radius; // Calc the circumference let circumference = 2*PI*radius; // Print the area console.log('The area of circle is: ' + area); // Print the circumference console.log('The circumference of circle is: '+ circumference);
Output:
The area of circle is: 78.5 The circumference of circle is: 31.400000000000002