# Kotlin Program: Lambda expression for finding the square of a number

## Kotlin Lambda: Exercise-2 with Solution

Write a Kotlin program that implements a lambda expression to find the square of a number and return the result.

**Sample Solution:**

**Kotlin Code:**

```
fun main() {
val square: (Int) -> Int = { num -> num * num }
val number = 7
val result = square(number)
println("The square of $number is: $result")
}
```

Sample Output:

The square of 7 is: 49

**Explanation:**

In the above exercise -

At first we declare a lambda expression square of type (Int) -> Int, which takes an integer num as an input parameter and returns its square (num * num). We assign the lambda expression to a variable square.

Inside the "main()" function, we define a number "number". We then invoke the square lambda expression by passing "number" as an argument and store the result in the result variable. Finally, we print the result.

**Kotlin Editor: **

**Previous:** Lambda expression for multiplying two numbers.

**Next:** Lambda expression to check if a number is even.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**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/lambda/kotlin-lambda-exercise-2.php

**Weekly Trends and Language Statistics**- Weekly Trends and Language Statistics