Kotlin Program to Find Roots of Quadratic Equation

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

In this post, we will go through Kotlin program to find roots of quadratic equation

What is a quadratic equation?

In algebra, a quadratic equation is any equation that can be expressed in standard form as
ax2 + bx + c = 0

Where, a, b and c are real numbers and a is not 0.
x is variable (Either real or complex value) for which this equation is satisfied.

We can find roots of equation (value of x that satisfy equation) using formula –

x = (-b ± √b2 -4ac) / 2a

It means,

First root is
x = (-b + √b2 -4ac) / 2a

and,

Second root is
x = (-b – √b2 -4ac) / 2a

Here, b2 – 4ac is called determinant that decides whether roots are real or imaginary (or complex). If

  • determinant > 0: Roots are real and different
  • determinant == 0: Roots are real and same
  • determinant < 0: Roots are imaginary (or, complex) and different

1. Kotlin Program to Find Roots of Quadratic Equation

Now, let’s see program in Kotlin that finds roots of the quadratic equation.

import java.util.*
import kotlin.math.sqrt

fun main() {

    val read = Scanner(System.`in`)

    println("To form quadratic equation a * x * x + b * x + c = 0")

    println("Enter a:")
    val a = read.nextDouble()

    println("Enter b:")
    val b = read.nextDouble()

    println("Enter c:")
    val c = read.nextDouble()

    val d: Double = (b * b) - (4 * a * c)       // This is determinant that decides whether roots are real or imaginary

    when {
        (d > 0) -> {
            val firstRoot = (-b + sqrt(d)) / ( 2 * a)
            val secondRoot = (-b - sqrt(d)) / ( 2 * a)

            val output = "Roots of equation a * x * x + b * x + c = 0 => x = %.2f and x = %.2f"
                    .format(firstRoot, secondRoot)
            println(output)
        }
        (d == 0.0) -> {
            val firstRoot = -b / ( 2 * a)

            val output = "Roots of equation a * x * x + b * x + c = 0 => x = %.2f and x = %.2f"
                    .format(firstRoot, firstRoot)
            println(output)
        }
        else -> {
            val realPart = -b / ( 2 * a)
            val imaginaryPart = sqrt(-d) / (2 * a)

            val output =
                "Roots of equation a * x * x + b * x + c = 0 => x = (%.2f + %.2fi) and x = (%.2f - %.2fi)"
                    .format(realPart, imaginaryPart, realPart, imaginaryPart)
            println(output)
        }
    }

}

When you run the program, output will be

To form quadratic equation a * x * x + b * x + c = 0
Enter a:
1
Enter b:
0
Enter c:
-1
Roots of equation a * x * x + b * x + c = 0 => x = 1.00 and x = -1.00

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextDouble() means read anything (i.e. Double value) entered by user before space or line break from standard input – Keyboard.

Input read by scanner is then stored in variable year

At first, we are taking value of a, b, and c from user to form quadratic equation.

Then, we find value of determinant and store it in variable d

Then, as per value of determinant we find the roots of equation.

In all process we are keeping in mind the formula to find roots of equation.

x = (-b ± √b2 -4ac) / 2a

Finally, we print result value using println() method.

Thus, we went through Kotlin Program to Find Roots of Quadratic Equation.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners