Quadratic equation in Scala
Example for versions
Simply Scala
This example shows that string processing and Math in Scala are implemented similarly to Java.
def output(real: Double, imag: Double): String =
if (imag == 0) ""+real
else "("+real+","+imag+")"
def solve(A: Int, B: Int, C: Int)
{ if (A == 0) print("Not a quadratic equation.")
else
{ def D = B*B - 4*A*C
if (D == 0) print("x = "+output(-B/2.0/A, 0))
else if (D > 0) print("x1 = "+output((-B+Math.sqrt(D))/2.0/A, 0)+"\nx2 = "+output((-B-Math.sqrt(D))/2.0/A, 0))
else print("x1 = "+output(-B/2/A, Math.sqrt(-D)/2.0/A)+"\nx2 = "+output(-B/2/A, -Math.sqrt(-D)/2.0/A))
}
}
Comments
]]>blog comments powered by Disqus
]]>