Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Program to find area of rectangle in Kotlin

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

Program:
fun main(args: Array)
{
    print("Enter the length of Rectangle : ")
    val length = readLine()!!
    print("Enter the width of Rectangle : ")
    val width = readLine()!!

    val area: Double
    if(length.toIntOrNull() != null && width.toIntOrNull() != null)
    {
        area = length.toDouble()  * width.toDouble();
        print("Area of Rectangle is : $area")
    }
    else
    {
        print("Please enter valid input.")
    }
}
Output:
Enter the length of Rectangle: 2
Enter the width of Rectangle: 4
Area of Rectangle is: 8.0
Program to find an area of the rectangle in Kotlin - Output

Description:
In this program, we will take input from the user to enter to provide the length and width value of the rectangle. 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.

To find the area of a rectangle we have used an equation length * width, which is equivalent to A = w * l. Finally, an area of the rectangle is printed using print() function.



This post first appeared on Ask For Program, please read the originial post: here

Share the post

Program to find area of rectangle in Kotlin

×

Subscribe to Ask For Program

Get updates delivered right to your inbox!

Thank you for your subscription

×