# Scala program to find the largest number among three numbers using ternary operator

Here, we are going to learn how to find the largest number among three numbers using ternary operator in Scala programming language?
Submitted by Nidhi, on April 26, 2021

Problem Solution:

Here, we will read the value of three integer variables from the user and find the largest among them using the ternary operator then print the largest number on the console screen.

Program/Source Code:

The source code to find the largest number among the three numbers using the ternary operator is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

```// Scala program to find the largest number among
// three numbers using ternary operator

object Sample{
def main(args:Array[String]){
var num1:Int=0
var num2:Int=0
var num3:Int=0
var large:Int=0

print("Enter number1: ")

print("Enter number2: ")

print("Enter number3: ")

large = if (num1 > num2 && num1 > num3) num1 else if(num2 > num1 && num2 > num3) num2 else num3

println("Largest number is: "+large)
}
}
```

Output:

```Enter number1: 10
Enter number2: 20
Largest number is: 20
```

Explanation:

In the above program, we used an object-oriented approach to create the program. Here, we created an object Sample. We defined main() function. The main() function is the entry point for the program.

In the main() function, we created three integer variables num1, num2num3. Then we read the value of variables from the user. After that, we found the largest number from them using the ternary operator and print the result on the console screen.

Note: In the Scala ternary operator using the if-else construct, Scala does not support the traditional ternary operator.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates