Program to find perimeter of a circle in Kotlin

In this article, we will learn simple Kotlin program to find the perimeter of a circle.

Source Code:
fun main(args: Array<String>)
{
    print("Enter circle radius : ")
    val input = readLine()!!
    val radius: Double
    if(input.toIntOrNull() != null)
    {
        radius = input.toDouble()
        val pi: Double=3.14159
        val perimeter: Double = 2 * pi * radius
        print("Perimeter of Circle is : $perimeter")
    }
    else
    {
        print("Please enter valid input.")
    }
}
Output:
Enter circle radius : 10
Perimeter of Circle is : 62.8318
Program to find the perimeter of a circle in Kotlin
Program to find the perimeter of a circle in Kotlin - Output

Description:
In the above program, we will take input from a user to know the radius of a circle. To do so we have used readLine() function. Next, we have checked that input accepted from the user is valid integer or decimal number. If an input is not valid then it will print the message to enter valid input.

If the input is a valid integer or decimal number, we will store it to a variable named val perimeter. Next, we have taken variable pi to hold the value of mathematical constant π, which is 3.14159. To find the perimeter of a circle we have used an equation 2 * pi * radius, which is equivalent to 2πr.

Post a Comment

0 Comments