Kotlin Program to Find all Roots of a Quadratic Equation

Kotlin Server Side ProgrammingProgramming

In this article, we will understand how to calculate the roots of a quadratic equation in Kotlin. A quadratic equation is an algebraic expression of the second degree or in other words, it has two results i.e., real number and an imaginary number.

Below is a demonstration of the same

Suppose our input is −

a = 1, b = 2, c = 3

The desired output would be −

The roots of the quadratic equation are
root1 = -1.00+1.41i
root2 = -1.00-1.41i

Algorithm

  • Step 1 − Start

  • Step 2 − Declare six values: inputA, inputB, inputC, root1, root2, myResult

  • Step 3 − Define the values

  • Step 4 − Compute ( inputB * inputB - 4.0 * inputA * inputC) and assign the result to myDeterminant

  • Step 5 − In a for loop, check if the value of myDeterminant variable is greater than 0, and if true, use quadric formula for root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) and for root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) to find the value, and assign it to a variable.

  • Step 6 − If the myDeterminant value is equal to 0, assign (-inputB / (2 * inputA)) to both root values.

  • Step 7 − If the myDeterminant value is less than 0, then compute val realPart = -inputB / (2 * inputA)

val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA)
  • Step 8 − Display the result

  • Step 9 − Stop

Example 1

In this example, we will find all the Roots of a Quadratic Equation. First, declare the input variables −

val inputA = 1.0
val inputB = 2.0
val inputC = 3.0

Now, set the variable for roots and the result −

val root1: Double
val root2: Double
val myResult: String

Calculate the Determinant using the formula −

b² - 4ac

Apply the above formulae to get the Determinant −

val myDeterminant = inputB * inputB - 4.0 * inputA * inputC

Use if…elseif…else to display all the roots of a Quadratic Equation −

if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) }

Let us now see the complete example −

fun main() { val inputA = 1.0 val inputB = 2.0 val inputC = 3.0 println("The input values are defined as $inputA, $inputB and $inputC ") val root1: Double val root2: Double val myResult: String val myDeterminant = inputB * inputB - 4.0 * inputA * inputC println("The roots of the quadratic equation are: ") if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) } println(myResult) }

Output

The input values are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are:
root1 = -1.00+1.41i and root2 = -1.00-1.41i

Example 2

In this example, we will find all the Roots of a Quadratic Equation using a custom function −

fun main() { val inputA = 1.0 val inputB = 2.0 val inputC = 3.0 println("The input values are defined as $inputA, $inputB and $inputC ") getRoots(inputA, inputB, inputC) } fun getRoots(inputA: Double, inputB: Double, inputC: Double) { val root1: Double val root2: Double val myResult: String val myDeterminant = inputB * inputB - 4.0 * inputA * inputC println("The roots of the quadratic equation are: ") if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) } println(myResult) }

Output

The input values are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are:
root1 = -1.00+1.41i and root2 = -1.00-1.41i
raja
Updated on 13-Oct-2022 13:13:35

Advertisements