Problem Statement:
Generate a List with input1
such that the number of elements is equal to input1
, and the numbers are from 1 to input1 (when input is 3 -> List(1,2,3)). Write a function findEven
using Option
(container) such that it returns boolean true
when the number is even, else returns None
. Using the for
loop, apply function findEven
on each element of the list, and using match
print if the number is even
or odd
.
Compile the program using: scalac Demo.scala
.
Execute the program to print the outputs: scala Demo 3
.
Ouput will be: 1 is odd. 2 is even. 3 is odd.
I have written code as below and getting the output as expected.But unable to complete the exam.
object Demo extends App {
val input1 :Int = args(0).toInt
val numberList :List[Int]=(1 to input1).flatMap((x:Int)=>List(x)).toList
def findeven (x:Int):Boolean =
{
if (x%2==0)
{return true}
else
{return false}
}
for (n <- numberList) {
print(n)
if(findeven(n)) {
println(" is even.")
}
else{
println(" is odd.")
}
}
}
Please guide me on this