Write a Scala program to find the number of even and odd integers in a given array of integers.
object scala_basic { def main(args: Array[String]): Unit = { var array_nums = Array(5, 7, 2, 4, 9) println("Original array:") for (x <- array_nums) { print(s"${x}, ") } var ctr = 0; for (i <- 0 to array_nums.length - 1) { if (array_nums(i) % 2 == 0) ctr=ctr+1 } println("\nNumber of even numbers : " + ctr); println("Number of odd numbers : " + (array_nums.length - ctr)); } }
Sample Output:
Original array: 5, 7, 2, 4, 9, Number of even numbers : 2 Number of odd numbers : 3
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer