Scala Programming: Find the even and odd numbers from a given list
Scala Programming List Exercise-13 with Solution
Write a Scala program to find the even and odd numbers from a given list.
Sample Solution:
Scala Code:
object Scala_List
{
def main(args: Array[String]): Unit =
{
val nums = List(1, 2, 3, 4, 5, 7, 9, 11, 14, 12, 16)
println("Original list:")
println(nums)
val even_nums = nums.filter(_ % 2 ==0)
println("Even number of the said list:")
println(even_nums)
val odd_nums = nums.filter(_ % 2 != 0)
println("Odd number of the said list:")
println(odd_nums)
}
}
Sample Output:
Original list: List(1, 2, 3, 4, 5, 7, 9, 11, 14, 12, 16) Even number of the said list: List(2, 4, 14, 12, 16) Odd number of the said list: List(1, 3, 5, 7, 9, 11)
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to merge (concatenate) given lists.
Next: Write a Scala program to find the nth element of a given list.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework