Q:

Write a Scala program to find the number of even and odd integers in a given array of integers

0

Write a Scala program to find the number of even and odd integers in a given array of integers.

All Answers

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

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

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