Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Scala Programming: Compute the sum of the two given integer values

Scala Programming Basic Exercise-2 with Solution

Write a Scala program to compute the sum of the two given integer values. If the two values are the same, then return triples their sum.

Sample Solution:

Scala Code:

object scala_basic {
  def test(x:Int, y:Int) : Int =
    {
        if (x == y) (x + y) * 3 else x + y
    }
     
   def main(args: Array[String]): Unit = {
      println("Result: " + test(1, 2));
      println("Result: " + test(2, 2));
   }
}

Sample Output:

Result: 3
Result: 12

Scala Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Scala program to print "Hello, world" and version of the Scala language.
Next: Write a Scala program to get the absolute difference between n and 51. If n is greater than 51 return triple the absolute difference.

What is the difficulty level of this exercise?