Kotlin program to find area of circle

Kotlin | Area of circle: Here, we are going to learn how to find the area of circle in Kotlin programming language? Submitted by IncludeHelp, on April 20, 2020

Problem statement

Given radius of the circle, we have to find the area of circle.

Example:

Input:
Radius: 3

Output:
Area of circle: 28.259999999999998

Kotlin - Find area of circle

To find the area of the circle, we use the formula: π r2.

Here,

  • π stands for PI which is a mathematical constant and it's value of 3.14, it defined as the ratio of a circle’s circumference to its diameter and it also has the various equivalent definitions.
  • r is the radius of the circle.

Program to find area of circle in Kotlin

package com.includehelp.basic

import java.util.*

// PI Constant Value
val PI = 3.14

/* function to calculate area of circle of given radius */
fun getAreaOfCirlce(radius: Double): Double {
    return PI * radius * radius
}

// Main Method Entry Point of Program
fun main(args:Array<String>){
    val sc = Scanner(System.`in`)
    
    // Input Radius
    println("Enter Circle Radius  : ")
    val radius = sc.nextDouble()
    
    // Call function to get Area of Circle
    val areaOfCircle = getAreaOfCirlce(radius)
    
    // Print Area of Circle
    println("Area of Circle : $areaOfCircle") 
}

Output

Run 1:
Enter Circle Radius  :
3
Area of Circle : 28.259999999999998
-----
Run 2:
Enter Circle Radius  :
8
Area of Circle : 200.96

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.