Scala Program to Find the Largest Among Three Numbers

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    In this article, we shall learn how to compare three numbers entered by a user, identify the largest number among them, and print the result.

    To implement this Scala program, the following are the prerequisites:

    • You should know how to take input from the user in scala.
    • You should know about If-else construct in Scala.

    To find out the largest among three numbers, we need to apply mathematics. What we generally do is that we observe all three numbers, and the one that is greater than the other two is the largest.

    We will try to use the same principle in our program with the help of the nested if-else construct of Scala.

    Example 1

    objectLargestFromThreeextendsApp {
    
    println("Please enter three integer number")
    val number1 = scala.io.StdIn.readInt()
    val number2 = scala.io.StdIn.readInt()
    val number3 = scala.io.StdIn.readInt()
    
    if (number1 >= number2) {
    if (number1 >= number3) {
    println(s"$number1 is greater")
    } else {
    println(s"$number3 is greater")
    }
    } else {
    if (number2 >= number3) {
    println(s"$number2 is greater")
    } else {
    println(s"$number3 is greater")
    }
    }
    
    }

    Output

    Sample Output 1:

    Please enter three integer number
    2
    56
    34
    56 is greater

    Sample Output 2:

    Please enter three integer number
    34
    34
    43
    43 is greater

    Explanation

    In the above program, initially, we have taken three numbers as input from the user. So, what we have done is that we have printed a simple message conveying the user to enter the three integers. After the user enters integers, we have then stored them in three different variables: number1, number2, and number3, respectively.

    The next step we performed in the above program was we started comparing two numbers and then compared the larger of the two numbers with the third number inside the nested if-else block.

    Note: One important thing to note here is that a user can also provide numbers where two of them, or say all three of the numbers, are equal. So, our logic should also work correctly for that scenario also.

    You can also write the above program by clubbing the nested condition into a single if block and write the above program just using an if-else ladder and using &&(And operator) like below.

    Example 2

    objectLargestFromThreeextendsApp {
    
    println("Please enter three integer number")
    val number1 = scala.io.StdIn.readInt()
    val number2 = scala.io.StdIn.readInt()
    val number3 = scala.io.StdIn.readInt()
    
    if (number1 >= number2 && number1 >= number3) {
    println(s"$number1 is greater")
    } elseif (number2 >= number1 && number2 >= number3) {
    println(s"$number2 is greater")
    } else {
    println(s"$number3 is greater")
    }
    
    }

    Output

    Please enter three integer number
    1
    2
    3
    3 is greater

    Note: The && operator, pronounced as AND operator, will give true as output when both the conditions are satisfied; otherwise, it will result into false.

    As per the above program, number1 is greater if number1 is greater than or equal to number2 and number1 is greater than number3. Similarly, number2 is greater if it is greater than both number1 and number3; otherwise, number 3 is the largest number.

    Conclusion

    This was all about implementing a Scala program to find out the largest among the three numbers. We have written the program for the same using two different methods, as you can see above. I recommend you try it on your own and understand the logic behind it by entering the different combinations of inputs.

    Keep practicing! Keep Coding!

    Leave a Comment on this Post

    0 Comments