Kotlin Program to Find all Roots of a Quadratic Equation

Kotlin Program to Find Roots of a Quadratic Equation: The roots of any quadratic equation is given by: x = [-b +/- sqrt(-b^2 – 4ac)]/2a.

Write down the quadratic in the form of ax^2 + bx + c = 0. If the equation is in the form y = ax^2 + bx +c, simply replace the y with 0. This is done because the roots of the equation are the values where the y-axis is equal to 0

Kotlin Program to Find Roots of a Quadratic Equation

Source Code

fun main(args: Array<String>) {

    val a = 2.3
    val b = 4
    val c = 5.6
    val root1: Double
    val root2: Double
    val output: String

    val determinant = b * b - 4.0 * a * c

    // condition for real and different roots
    if (determinant > 0) {
        root1 = (-b + Math.sqrt(determinant)) / (2 * a)
        root2 = (-b - Math.sqrt(determinant)) / (2 * a)

        output = "root1 = %.2f and root2 = %.2f".format(root1, root2)
    }
    // Condition for real and equal roots
    else if (determinant == 0.0) {
        root2 = -b / (2 * a)
        root1 = root2

        output = "root1 = root2 = %.2f;".format(root1)
    }
    // If roots are not real
    else {
        val realPart = -b / (2 * a)
        val imaginaryPart = Math.sqrt(-determinant) / (2 * a)

        output = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart)
    }

    println(output)
}

Output