Write a Program in JavaScript to Find Area of Circle | Area of Circle in JavaScript
ADVERTISEMENTS
Write a program in javascript to find area of circle.
In this article, you will learn how to write a program in javascript to find area of circle.
Formula
D = 2 * r
C = 2 * PI * r
A = PI * r2
Where
R = radius of the circle
D = diameter of the circle
C = circumference of the circle
A = area of the circle
There are two ways to implement these formulae:
- By using the default value of PI = 3.14
- Second, by using Math.PI constant
1. By using the default value of PI = 3.14
/* Write a Program in JavaScript to Find Area of Circle | Area of Circle in JavaScript */
<script type="text/javascript">
var r, d, c, a;
// r = radius
// d = diameter
// c = circumference
// a = area
/* Calculation of diameter, circumference, and area */
r = 10;
d = 2 * 10;
c = 2 * 3.14 * 10;
a = 3.14 * (10 * 10);
document.write("Diameter = " + d + " units<br />");
document.write("Circumference = " + c + " units<br />");
document.write("Area = " + a + " sq. units");
</script>
Output
Diameter = 20 units
Circumference = 62.800000000000004 units
Area = 314 sq. units
2. By using Math.PI constant
/* Write a Program in JavaScript to Find Area of Circle | Area of Circle in JavaScript */
<script type="text/javascript">
var r, d, c, a;
// r = radius
// d = diameter
// c = circumference
// a = area
/* Calculation of diameter, circumference, and area */
r = 10;
d = 2 * 10;
c = 2 * Math.PI * 10;
a = Math.PI * (10 * 10);
document.write("Diameter = " + d + " units<br />");
document.write("Circumference = " + c + " units<br />");
document.write("Area = " + a + " sq. units");
</script>
Output
Diameter = 20 units
Circumference = 62.83185307179586 units
Area = 314.1592653589793 sq. units