Sling Academy
Home/Kotlin/How to Solve Quadratic Equations in Kotlin

How to Solve Quadratic Equations in Kotlin

Last updated: November 30, 2024

Introduction

Quadratic equations are a type of polynomial equation that you can solve mathematically and programmatically. In this article, we will go through how to solve quadratic equations specifically using the Kotlin programming language.

What is a Quadratic Equation?

A quadratic equation is a second-order polynomial equation in a single variable x, with a non-zero coefficient for x2. It has the form:

a * x2 + b * x + c = 0

Here, a, b, and c are constants with a ≠ 0.

The Quadratic Formula

The solutions to the quadratic equation are given by the quadratic formula:

x = (-b ± sqrt(b2 - 4 * a * c)) / (2 * a)

The term under the square root (b^2 - 4ac) is known as the discriminant.

Implementing in Kotlin

Now, let's implement a simple function in Kotlin to find the roots of a quadratic equation using the quadratic formula.

Step 1: Declare the Function

First, we'll start by declaring a function named solveQuadratic.


fun solveQuadratic(a: Double, b: Double, c: Double): Pair {
    // function implementation
}

Step 2: Calculate the Discriminant

Inside the function, calculate the discriminant:


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

Step 3: Analyze the Discriminant

The nature of the roots can be determined from the discriminant:

  • If the discriminant is positive, the equation has two different real roots.
  • If the discriminant is zero, the equation has two equal real roots.
  • If the discriminant is negative, the equation has no real roots (but two complex roots).

Step 4: Calculate the Roots

Calculate and return the roots based on the discriminant:


when {
    discriminant > 0 -> {
        val root1 = (-b + kotlin.math.sqrt(discriminant)) / (2 * a)
        val root2 = (-b - kotlin.math.sqrt(discriminant)) / (2 * a)
        return Pair(root1, root2)
    }
    discriminant == 0.0 -> {
        val root = -b / (2 * a)
        return Pair(root, root)
    }
    else -> {
        return Pair(null, null) // For complex roots
    }
}

Putting It All Together

Here's the complete code for solving a quadratic equation:


fun solveQuadratic(a: Double, b: Double, c: Double): Pair {
    val discriminant = b * b - 4 * a * c
    return when {
        discriminant > 0 -> {
            val root1 = (-b + kotlin.math.sqrt(discriminant)) / (2 * a)
            val root2 = (-b - kotlin.math.sqrt(discriminant)) / (2 * a)
            Pair(root1, root2)
        }
        discriminant == 0.0 -> {
            val root = -b / (2 * a)
            Pair(root, root)
        }
        else -> Pair(null, null) 
    }
}

Conclusion

With this Kotlin implementation, you are able to solve any quadratic equation. You just need to call the solveQuadratic function with the appropriate coefficients. Happy coding!

Next Article: Combining Regex and Math for Input Validation in Kotlin

Previous Article: Working with Prime Numbers in Kotlin

Series: Primitive data types in Kotlin

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin