Kotlin Program to Find all Roots of a Quadratic Equation

Solving quadratic equations and finding their roots is a fundamental concept in algebra and mathematics. Kotlin, being a versatile programming language, offers various ways to find all the roots of a quadratic equation efficiently. In this article, we’ll explore three Kotlin Program to Find all Roots of a Quadratic Equation using different solutions and showcase their outputs.

1. Using Quadratic Formula

This example demonstrates how to find all roots of a quadratic equation using the quadratic formula. We take the coefficients of the quadratic equation as input and calculate the discriminant to determine the nature of the roots.

Code

Kotlin
import kotlin.math.sqrt

fun main() {
    val a = 1.0
    val b = -3.0
    val c = 2.0

    val discriminant = b * b - 4 * a * c

    if (discriminant > 0) {
        val root1 = (-b + sqrt(discriminant)) / (2 * a)
        val root2 = (-b - sqrt(discriminant)) / (2 * a)
        println("Roots are real and distinct: $root1 and $root2")
    } else if (discriminant == 0.0) {
        val root = -b / (2 * a)
        println("Roots are real and equal: $root")
    } else {
        val realPart = -b / (2 * a)
        val imaginaryPart = sqrt(-discriminant) / (2 * a)
        println("Roots are complex and distinct: $realPart + $imaginaryPart i and $realPart - $imaginaryPart i")
    }
}

1.3 Output

Kotlin
Roots are real and distinct: 2.0 and 1.0

2. Using Functions for Root Calculation

In this example, we define functions to calculate the discriminant and roots of a quadratic equation. This approach enhances code readability and reusability.

Code

Kotlin
import kotlin.math.sqrt

fun calculateDiscriminant(a: Double, b: Double, c: Double): Double {
    return b * b - 4 * a * c
}

fun calculateRoots(a: Double, b: Double, c: Double) {
    val discriminant = calculateDiscriminant(a, b, c)

    if (discriminant > 0) {
        val root1 = (-b + sqrt(discriminant)) / (2 * a)
        val root2 = (-b - sqrt(discriminant)) / (2 * a)
        println("Roots are real and distinct: $root1 and $root2")
    } else if (discriminant == 0.0) {
        val root = -b / (2 * a)
        println("Roots are real and equal: $root")
    } else {
        val realPart = -b / (2 * a)
        val imaginaryPart = sqrt(-discriminant) / (2 * a)
        println("Roots are complex and distinct: $realPart + $imaginaryPart i and $realPart - $imaginaryPart i")
    }
}

fun main() {
    val a = 1.0
    val b = -3.0
    val c = 2.0

    calculateRoots(a, b, c)
}

Output

Kotlin
Roots are real and distinct: 2.0 and 1.0

3. Using Quadratic Equation Class

Here, we define a QuadraticEquation class with properties for coefficients and functions to calculate discriminant and roots. This object-oriented approach encapsulates the logic related to quadratic equations.

Code

Kotlin
import kotlin.math.sqrt

class QuadraticEquation(private val a: Double, private val b: Double, private val c: Double) {
    fun calculateDiscriminant(): Double {
        return b * b - 4 * a * c
    }

    fun calculateRoots() {
        val discriminant = calculateDiscriminant()

        if (discriminant > 0) {
            val root1 = (-b + sqrt(discriminant)) / (2 * a)
            val root2 = (-b - sqrt(discriminant)) / (2 * a)
            println("Roots are real and distinct: $root1 and $root2")
        } else if (discriminant == 0.0) {
            val root = -b / (2 * a)
            println("Roots are real and equal: $root")
        } else {
            val realPart = -b / (2 * a)
            val imaginaryPart = sqrt(-discriminant) / (2 * a)
            println("Roots are complex and distinct: $realPart + $imaginaryPart i and $realPart - $imaginaryPart i")
        }
    }
}

fun main() {
    val quadraticEquation = QuadraticEquation(1.0, -3.0, 2.0)
    quadraticEquation.calculateRoots()
}

Output

Kotlin
Roots are real and distinct: 2.0 and 1.0

Conclusion

Finding all roots of a quadratic equation is a common mathematical problem, and Kotlin provides various solutions to tackle it effectively. Whether using the quadratic formula directly, defining functions for root calculation, or encapsulating the logic in a class, understanding these solutions enhances your Kotlin programming skills and prepares you for handling mathematical computations efficiently in your applications. Each example presented here offers a unique perspective on solving quadratic equations, giving you the flexibility to choose the most suitable approach based on your specific requirements.