Q:

Write a Scala program to find the even and odd numbers from a given list

0

Write a Scala program to find the even and odd numbers from a given list.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

zobject 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)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now