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

Source Code:

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.

Source Code:

fun main(args: Array<String>)

{

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:{

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.")

}

}

Enter the length of Rectangle: 2

Enter the width of Rectangle: 4

Area of Rectangle is: 8.0

Description:Enter the width of Rectangle: 4

Area of Rectangle is: 8.0

Program to find an area of the rectangle in Kotlin - Output |

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.

## 0 Comments