- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell program to find all roots of a quadratic equation
In this tutorial, we discuss writing a program to find the largest among the three numbers Haskell programming language. A quadratic equation is a second-degree algebraic equation.
Example − a quadratic equation is of the form ax^2 + bx + c, where a,b, and c are constants and x is variable. A quadratic equation has two roots.
Algorithm steps
Take a quadratic equation as input.
Implement the programming logic to compute the roots.
Display the roots.
Algorithm Logic
Take input or initialize values for a,b, and c. As the quadratic expression can be constructed with these values (ax^2 + bx + c).
Compute Discriminant D which is equal to b^2 - 4*a*c.
If Discriminant D is greater than or equal to zero then the roots are
root1 = (-b + sqrt(D))/2*a
root2 = (-b - sqrt(D))/2*a
We will print the roots.
If Discriminant D is less than zero then the roots are imaginary and we print the root are imaginary
Note− If the Discriminant D is zero the roots are -b/2*a and -b/2*a.
Example
Program to find all roots of a quadratic equation
-- function declaration for function printRoots printRoots :: Float->Float->Float->IO() -- function definition for function printRoots printRoots a b c = do print("a,b,c values =",a,b,c) let d = b^2 - 4*a*c -- let d = 1 if (d < 0) then do print ("This quadratic equation has imaginary roots") else do let root1 = (-(b) + sqrt (d))/2 * (a) let root2 = (-(b) - sqrt (d))/2 * (a) print ("The roots for this quadratic are:") print (root1) print (root2) main :: IO() main = do -- declaring and initializing constants in quadratic equation let a = 1 let b = 4 let c = 2 -- invoking the printRoots function printRoots (a) (b) (c)
Output
("a,b,c values =",1.0,4.0,2.0) "The roots for this quadratic are:" -0.58578646 -3.4142137
In the above program, we declared a function printRoots as such it takes three floats as argument and returns an IO action. In its function definition, we are taking three arguments a, b, and c. We are computing the discriminant by the formula b^2 - 4*a*c and loading the discriminant into a variable d. We are checking if the discriminant is less than zero, we are printing the statement “roots are imaginary” as we know that if the discriminant is less than zero for a quadratic equation the roots are imaginary. If the discriminant is greater than zero, we are computing the roots using the formula (-b +/- sqrt(d))/2*a. We are loading the roots into variables root1 and root2. And finally, we printed the roots in the function. In the main function, we declared and initialized variables a, b and c. Finally, we invoked the function printRoots with the three numbers as the arguments.
Conclusion
In this tutorial, we discussed implementing a program to find the roots of a quadratic equation in Haskell programming Language.
- Related Articles
- C++ Program to Find All Roots of a Quadratic Equation
- Java Program to Find all Roots of a Quadratic Equation
- Kotlin Program to Find all Roots of a Quadratic Equation
- How to Find all Roots of a Quadratic Equation in Golang?
- Java program to find the roots of a quadratic equation
- C program to find the Roots of Quadratic equation
- How to write a C program to find the roots of a quadratic equation?
- Finding roots of a quadratic equation – JavaScript
- Write all the values of k for which the quadratic equation $x^2+kx+16=0$ has equal roots. Find the roots of the equation so obtained.
- Find the quadratic roots in the equation$4x^{2}-3x+7$
- Program to find number of solutions in Quadratic Equation in C++
- Find $p$, if quadratic equation $py( y-2)+6=0$ has equal roots.
- A quadratic equation with integral coefficient has integral roots. Justify your answer.
- Find the roots of the following quadratic equation:$x^{2} -3\sqrt {5}\ x+10=0$
- Find the roots of the quadratic equation $\sqrt{2}x^{2}+7x+5\sqrt{2}=0$.
