Kotlin: Find the Roots of a Quadratic Equation

1. Introduction

Quadratic equations are foundational in algebra. Essentially, a quadratic equation represents a parabola and when graphed, this curve may cross, touch, or float above/below the x-axis. The points where the parabola intersects or touches the x-axis are called its roots. In this blog post, we'll look at how to determine these roots using Kotlin.

2. Program Overview

Understanding Quadratic Equations 

Before diving into the Kotlin code, it's helpful to have a basic understanding. A quadratic equation can be represented as: 
ax2+bx+c=0 
Where a, b, and c are coefficients. Depending on the values of these coefficients, a quadratic equation might have: 
  • Two distinct real roots 
  • One real root (also known as a repeated or double root) 
  • No real roots

Program Steps

Our Kotlin sequence will:

1. Prompt the user to input the coefficients of the quadratic equation.

2. Compute the discriminant to judge the nature of the roots.

3. Depending on the discriminant, determine and display the roots.

3. Code Program

import java.util.Scanner
import kotlin.math.sqrt

fun main() {
    // Activate Scanner for user input
    val reader = Scanner(System.in)

    // Solicit the coefficients of the quadratic equation from the user
    print("Enter coefficient a: ")
    val a = reader.nextDouble()
    print("Enter coefficient b: ")
    val b = reader.nextDouble()
    print("Enter coefficient c: ")
    val c = reader.nextDouble()

    // Compute the discriminant
    val discriminant = b * b - 4 * a * c

    // Decide the nature of the roots based on the discriminant
    when {
        discriminant > 0 -> {
            val root1 = (-b + sqrt(discriminant)) / (2 * a)
            val root2 = (-b - sqrt(discriminant)) / (2 * a)
            println("Two distinct real roots exist: $root1 and $root2")
        }
        discriminant == 0.0 -> {
            val root = -b / (2 * a)
            println("Two equal and real roots exist: $root and $root")
        }
        else -> {
            val realPart = -b / (2 * a)
            val imaginaryPart = sqrt(-discriminant) / (2 * a)
            println("Complex roots exist: $realPart + $imaginaryPart i and $realPart - $imaginaryPart i")
        }
    }
}

Output:

Enter coefficient a: 1
Enter coefficient b: -3
Enter coefficient c: 2
Two distinct real roots exist: 2.0 and 1.0

4. Step By Step Explanation

1. Scanner Initialization: We start by engaging the Scanner class to take user input, naming our instance reader.

2. Coefficients Collection: We request the user to provide the three coefficients of the quadratic equation (a, b, and c).

3. Computing the Discriminant: The discriminant (often represented as Δ or D) plays a pivotal role in determining the nature of the roots. It's calculated using the formula b^2 - 4ac.

4. Roots Calculation: Depending on the value of the discriminant, the nature of the roots is ascertained:

- If Δ > 0, two distinct real roots exist.- If Δ = 0, two equal and real roots exist.- If Δ < 0, two complex roots exist.

5. Displaying the Roots: Once the roots are calculated, they're showcased to the user using the println function.

Comments