Home »
Code Examples »
Scala Code Examples
Scala - Ternary Operator (Find the given number is POSITIVE or NEGATIVE) Code Example
The code for Ternary Operator (Find the given number is POSITIVE or NEGATIVE)
// Scala program to find the given number is positive or negative
// using ternary operator
object Sample{
def main(args:Array[String]){
var num:Int=0
var result:String=""
print("Enter number: ")
num = scala.io.StdIn.readInt()
result = if (num>=0) "POSITVE" else "NEGATIVE"
println("Number is: "+result)
}
}
/*
Output:
Enter number: -8
Number is: NEGATIVE
*/
Code by IncludeHelp,
on August 12, 2022 23:24