w3resource

Kotlin program: Calculate circle area and perimeter

Kotlin Basic: Exercise-10 with Solution

Write a Kotlin program to calculate the area and perimeter of a circle.

Sample Solution:

Kotlin Code:

import kotlin.math.PI

fun main(args: Array) 
  {
    println("Input the radius of the circle:")
    val radius = args[0].toDoubleOrNull()

    if (radius != null && radius > 0) 
      {
        val area = calculateArea(radius)
        val perimeter = calculatePerimeter(radius)

        println("Radius of the circle: $radius")
        println("Area of the circle: $area")
        println("Perimeter of the circle: $perimeter")
     } 
      else 
      {
        println("Invalid input. Please enter a valid positive radius.")
     }
 }

fun calculateArea(radius: Double): Double {
    return PI * radius * radius
}

fun calculatePerimeter(radius: Double): Double {
    return 2 * PI * radius
}

Sample Output:

Input the radius of the circle:
Radius of the circle: 5.2
Area of the circle: 84.94866535306801
Perimeter of the circle: 32.67256359733385

Kotlin Editor:


Previous: Check if a year is a leap year.
Next: Kotlin Program for Celsius to Fahrenheit and Vice Versa.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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/basic/kotlin-basic-exercise-10.php