How to get the perimeter of a circle in Scala
Overview
The perimeter of a circle is the distance around a closed figure, which in this case is a circle. It is also known as the circumference. It is the product of 2, pi, and the circle's radius.
All we need to do to find this value is to create a constant called pi using the val keyword. Next, we multiply this with other values.
Syntax
2 * PI * radius
Syntax of perimeter of a circle in scala
Parameters
PI: This is pi in mathematics. Its value is3.14.
radius: This is the radius specified.
Return value
The value returned is a float, which is the total distance around the circle.
Example
// import the math moduleimport scala.math._object Main extends App {// create a PI valueval PI = Math.PI// create some radiivar rad1 = 3var rad2 = 45var rad3 = 1.5var rad4 = 0.1// get the perimeter of criclesvar per1 = 2 * PI * rad1var per2 = 2 * PI * rad2var per3 = 2 * PI * rad3var per4 = 2 * PI * rad4// print the perimetersprintln("The perimeter of area of radius "+rad1+" is: "+per1)println("The perimeter of area of radius "+rad2+" is: "+per2)println("The perimeter of area of radius "+rad3+" is: "+per3)println("The perimeter of area of radius "+rad4+" is: "+per4)}
Explanation
- Line 2: We import the
Mathmodule. - Line 6: We create a
PIconstant value. - Lines 9–12: We create some radius values.
- Lines 15–18: We get the perimeter of circles with the radius created.
- Lines 21–24: We print the perimeters.