Max Min in Scala

Find this useful? Support us: Star on GitHub 6
Category: Math | Language: Scala

In Scala, we can find the maximum and minimum of two numbers using the max() and min() methods respectively. Here are the examples:

// finding the maximum of two numbers
val a = 5
val b = 10
val max = a.max(b)
println("The maximum of " + a + " and " + b + " is " + max)

// Output: The maximum of 5 and 10 is 10

// finding the minimum of two numbers
val num1 = 250
val num2 = 350
val min = num1.min(num2)
println("The minimum of " + num1 + " and " + num2 + " is " + min)

// Output: The minimum of 250 and 350 is 250

In the first example, we have defined two numbers a and b. We then use the max() method to find the maximum of these two numbers and store the result in a variable max, which is then printed to the console.

In the second example, we have defined two numbers num1 and num2. We then use the min() method to find the minimum of these two numbers and store the result in a variable min, which is then printed to the console.