Scala Programming: Check two numbers, 4 or 7 present in a given array of integers
Scala Programming Array Exercise-13 with Solution
Write a Scala program to check two numbers, 4 or 7 present in a given array of integers.
Sample Solution:
Scala Code:
object Scala_Array {
def test(arr: Array[Int]): Boolean = {
arr.exists(x => x == 4 || x == 7)
}
def main(args: Array[String]): Unit = {
var nums1 = Array(2,4,5,7,9)
println("Orginal array:")
for ( x <- nums1) {
print(s"${x}, ")
}
println("\nIf 4 or 7 present in the said array? "+test(nums1))
var nums2 = Array(2,5,5,6,9)
println("Orginal array:")
for ( x <- nums2) {
print(s"${x}, ")
}
println("\nIf 4 or 7 present in the said array? "+test(nums2))
var nums3 = Array(2,5,7,6,9)
println("Orginal array:")
for ( x <- nums3) {
print(s"${x}, ")
}
println("\nIf 4 or 7 present in the said array? "+test(nums3))
}
}
Sample Output:
Console (F3) Orginal array: 2, 4, 5, 7, 9, If 4 or 7 present in the said array? true Orginal array: 2, 5, 5, 6, 9, If 4 or 7 present in the said array? false Orginal array: 2, 5, 7, 6, 9, If 4 or 7 present in the said array? true
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Scala program to reverse an array of integer values.
Next: Write a Scala program to find the maximum value from first, middle and last values of a given array of integers. Array length should be 1 and more and odd.
What is the difficulty level of this exercise?
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/array/scala-array-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics