w3resource

Find the largest and smallest number from a given list

Scala Programming List Exercise-6 with Solution

Write a Scala program to find the largest and smallest number from a given list.

Sample Solution:

Scala Code:

object Scala_List
{
def main(args: Array[String]): Unit = 
 {
   //Iterate over a list
   val nums = List(1, 3, 5, 7, 9, 11, 14, 12)
   println("Original list:")
   println(nums)   
   println("Largest number of the said list:")
   println(nums.max)
   println("Smallest number from the said list:")
   println(nums.min)
  }
}

Sample Output:

Original list:
List(1, 3, 5, 7, 9, 11, 14, 12)
Largest number of the said list:
14
Smallest number from the said list:
1

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to iterate over a list to print the elements and calculate the sum and product of all elements of this list.
Next: Write a Scala program to remove duplicates from a given list.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://w3resource.com/scala-exercises/list/scala-list-exercise-6.php