# Kotlin function: Calculate circle area with default pi

## Kotlin Function: Exercise-9 with Solution

Write a Kotlin function that calculates the area of a circle. Use a default value of 3.14 for pi.

Sample Solution:

Kotlin Code:

``````fun calculateCircleArea(radius: Double, pi: Double = 3.14): Double {
}

fun main() {
}
```
```

Sample Output:

```The area of the circle with radius 4.0 is 50.24
```

Explanation:

In the above function -

• The "calculateCircleArea()" function takes two parameters: radius (representing the circle radius) and pi (representing the value of pi). The pi parameter has a default value of 3.14.
• Within the function, the circle area is calculated by multiplying the square of the radius by pi.
• The calculated area is returned as a Double value.
• In the "main()" function, a sample radius of 5.0 is defined.
• The "calculateCircleArea()" function is called with the radius as an argument, and the result is stored in the area variable.
• The circle area is then printed to the console using string interpolation.

Kotlin Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/kotlin-exercises/function/kotlin-function-exercise-9.php