# Kotlin Program to Find all Roots of a Quadratic Equation

#### In this program, you’ll learn to find all roots of a quadratic equation (depending upon the determinant) and print them using format() in Kotlin.

The standard form of a quadratic equation is:

```ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
```

The term `b2-4ac` is known as the determinant of a quadratic equation. The determinant tells the nature of the roots.

• If determinant is greater than 0, the roots are real and different.
• If determinant is equal to 0, the roots are real and equal.
• If determinant is less than 0, the roots are complex and different.

## Example: Kotlin Program to Find Roots of a Quadratic Equation

``````
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)
}``````

When you run the program, the output will be:

`root1 = -0.87+1.30i and root2 = -0.87-1.30i`

In the above program, the coefficients ab and c are set to 2.3, 4 and 5.6 respectively. Then, the `determinant` is calculated as `b2 - 4ac`.

Based on the value of determinant, the roots are calculated as given in the formula above. Notice we’ve used library function Math.sqrt() to calculate the square root of a number.

The output to be printed is then stored in a string variable output using the Kotlin’s standard libary function `format()`. The output is then printed using `println()`.

# Special 95% discount

## Two Machine Learning Fields

There are two sides to machine learning:

• Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
• Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

`Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.  `