Program to find the Roots of a Quadratic Equation
Given a quadratic equation in the form ax2 + bx + c, (Only the values of a, b and c are provided) the task is to find the roots of the equation.
Examples:
Input: a = 1, b = -2, c = 1
Output: Roots are real and same 1Input : a = 1, b = 7, c = 12
Output: Roots are real and different
-3, -4Input : a = 1, b = 1, c = 1
Output : Roots are complex
-0.5 + i1.73205, -0.5 – i1.73205
Roots of Quadratic Equation using Sridharacharya Formula:
The roots could be found using the below formula (It is known as the formula of Sridharacharya)
[Tex]x=\frac{-b\pm \sqrt{b^2-4ac}}{2a} [/Tex]
The values of the roots depends on the term (b2 – 4ac) which is known as the discriminant (D).
If D > 0:
=> This occurs when b2 > 4ac.
=> The roots are real and unequal.
=> The roots are {-b + ?(b2 – 4ac)}/2a and {-b – ?(b2 – 4ac)}/2a.If D = 0:
=> This occurs when b2 = 4ac.
=> The roots are real and equal.
=> The roots are (-b/2a).If D < 0:
=> This occurs when b2 < 4ac.
=> The roots are imaginary and unequal.
=> The discriminant can be written as (-1 * -D).
=> As D is negative, -D will be positive.
=> The roots are {-b ± ?(-1*-D)} / 2a = {-b ± i?(-D)} / 2a = {-b ± i?-(b2 – 4ac)}/2a where i = ?-1.
Use the following pseudo algorithm to find the roots of the
Pseudo algorithm:
Start
Read the values of a, b, c
Compute d = b2 – 4ac
If d > 0
calculate root1 = {-b + ?(b2 – 4ac)}/2a
calculate root2 = {-b – ?(b2 – 4ac)}/2a
else If d = 0
calculate root1 = root2 = (-b/2a)
else
calculate root1 = {-b + i?-(b2 – 4ac)}/2a
calculate root2 = {-b + i?-(b2 – 4ac)}/2a
print root1 and root2
End
Below is the implementation of the above formula.
// C++ program to find roots of a quadratic equation
#include <bits/stdc++.h>
using namespace std;
// Prints roots of quadratic equation ax*2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not quadratic, but
// linear
if (a == 0) {
cout << "Invalid";
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
if (d > 0) {
cout << "Roots are real and different \n";
cout << (double)(-b + sqrt_val) / (2 * a) << "\n"
<< (double)(-b - sqrt_val) / (2 * a);
}
else if (d == 0) {
cout << "Roots are real and same \n";
cout << -(double)b / (2 * a);
}
else // d < 0
{
cout << "Roots are complex \n";
cout << -(double)b / (2 * a) << " + i"
<< sqrt_val / (2 * a) << "\n"
<< -(double)b / (2 * a) << " - i"
<< sqrt_val / (2 * a);
}
}
// Driver code
int main()
{
int a = 1, b = -7, c = 12;
// Function call
findRoots(a, b, c);
return 0;
}
// C program to find roots of a quadratic equation
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
// Prints roots of quadratic equation ax*2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not quadratic, but
// linear
if (a == 0) {
printf("Invalid");
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
if (d > 0) {
printf("Roots are real and different \n");
printf("%f\n%f", (double)(-b + sqrt_val) / (2 * a),
(double)(-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
printf("Roots are real and same \n");
printf("%f", -(double)b / (2 * a));
}
else // d < 0
{
printf("Roots are complex \n");
printf("%f + i%f\n%f - i%f", -(double)b / (2 * a),
sqrt_val / (2 * a), -(double)b / (2 * a),
sqrt_val / (2 * a));
}
}
// Driver code
int main()
{
int a = 1, b = -7, c = 12;
// Function call
findRoots(a, b, c);
return 0;
}
// Java program to find roots
// of a quadratic equation
import static java.lang.Math.*;
import java.io.*;
class Quadratic {
// Prints roots of quadratic
// equation ax * 2 + bx + x
static void findRoots(int a, int b, int c)
{
// If a is 0, then equation is not
// quadratic, but linear
if (a == 0) {
System.out.println("Invalid");
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = sqrt(abs(d));
if (d > 0) {
System.out.println(
"Roots are real and different \n");
System.out.println(
(double)(-b + sqrt_val) / (2 * a) + "\n"
+ (double)(-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
System.out.println(
"Roots are real and same \n");
System.out.println(-(double)b / (2 * a) + "\n"
+ -(double)b / (2 * a));
}
else // d < 0
{
System.out.println("Roots are complex \n");
System.out.println(-(double)b / (2 * a) + " + i"
+ sqrt_val / (2 * a) + "\n"
+ -(double)b / (2 * a)
+ " - i"
+ sqrt_val / (2 * a));
}
}
// Driver code
public static void main(String args[])
{
int a = 1, b = -7, c = 12;
// Function call
findRoots(a, b, c);
}
}
// This code is contributed by Sumit Kumar.
# Python program to find roots
# of a quadratic equation
import math
# Prints roots of quadratic equation
# ax*2 + bx + x
def findRoots(a, b, c):
# If a is 0, then equation is
# not quadratic, but linear
if a == 0:
print("Invalid")
return -1
d = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(d))
if d > 0:
print("Roots are real and different ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif d == 0:
print("Roots are real and same")
print(-b / (2*a))
else: # d<0
print("Roots are complex")
print(- b / (2*a), " + i", sqrt_val / (2 * a))
print(- b / (2*a), " - i", sqrt_val / (2 * a))
# Driver Program
if __name__ == '__main__':
a = 1
b = -7
c = 12
# Function call
findRoots(a, b, c)
# This code is contributed by Sharad Bhardwaj.
// C# program to find roots
// of a quadratic equation
using System;
class Quadratic {
// Prints roots of quadratic
// equation ax * 2 + bx + x
void findRoots(int a, int b, int c)
{
// If a is 0, then equation is
// not quadratic, but linear
if (a == 0) {
Console.Write("Invalid");
return;
}
int d = b * b - 4 * a * c;
double sqrt_val = Math.Abs(d);
if (d > 0) {
Console.Write(
"Roots are real and different \n");
Console.Write(
(double)(-b + sqrt_val) / (2 * a) + "\n"
+ (double)(-b - sqrt_val) / (2 * a));
}
// d < 0
else {
Console.Write("Roots are complex \n");
Console.Write(-(double)b / (2 * a) + " + i"
+ sqrt_val / (2 * a) + "\n"
+ -(double)b / (2 * a) + " - i"
+ sqrt_val / (2 * a));
}
}
// Driver code
public static void Main()
{
Quadratic obj = new Quadratic();
int a = 1, b = -7, c = 12;
// Function call
obj.findRoots(a, b, c);
}
}
// This code is contributed by nitin mittal.
<script>
// JavaScript program to find roots
// of a quadratic equation
// Prints roots of quadratic
// equation ax * 2 + bx + x
function findRoots(a, b, c)
{
// If a is 0, then equation is not
// quadratic, but linear
if (a == 0) {
document.write("Invalid");
return;
}
let d = b * b - 4 * a * c;
let sqrt_val = Math.sqrt(Math.abs(d));
if (d > 0) {
document.write(
"Roots are real and different \n" + "<br/>");
document.write(
(-b + sqrt_val) / (2 * a) + "<br/>"
+ (-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
document.write(
"Roots are real and same \n" + "<br/>");
document.write(-b / (2 * a) + "<br/>"
+ -b / (2 * a)) ;
}
else // d < 0
{
document.write("Roots are complex \n");
document.write(-b / (2 * a) + " + i"
+ sqrt_val / (2 * a) + "<br/>"
+ -b / (2 * a)
+ " - i" + sqrt_val) / (2 * a) ;
}
}
// Driver Code
let a = 1, b = -7, c = 12;
// Function call
findRoots(a, b, c);
</script>
<?php
// PHP program to find roots
// of a quadratic equation
// Prints roots of quadratic
// equation ax*2 + bx + x
function findRoots($a, $b, $c)
{
// If a is 0, then equation is
// not quadratic, but linear
if ($a == 0)
{
echo "Invalid";
return;
}
$d = $b * $b - 4 * $a * $c;
$sqrt_val = sqrt(abs($d));
if ($d > 0)
{
echo "Roots are real and ".
"different \n";
echo (-$b + $sqrt_val) / (2 * $a) , "\n",
(-$b - $sqrt_val) / (2 * $a);
}
else if ($d == 0)
{
echo "Roots are real and same \n";
echo -$b / (2 * $a);
}
// d < 0
else
{
echo "Roots are complex \n";
echo -$b / (2 * $a) , " + i" ,
$sqrt_val / (2 * $a) , "\n" , -$b / (2 * $a),
" - i", $sqrt_val / (2 * $a) ;
}
}
// Driver code
$a = 1; $b = -7 ;$c = 12;
// Function call
findRoots($a, $b, $c);
// This code is contributed
// by nitin mittal.
?>
Time Complexity: O(log(D)), where D is the discriminant of the given quadratic equation.
Auxiliary Space: O(1)
Using Formula in python:
Approach:
- Import the math module for square root and other mathematical operations.
- Declare the coefficients a, b, and c of the quadratic equation.
- Calculate the discriminant discriminant using the formula b^2 – 4ac.
- Check if the discriminant is greater than zero, zero, or less than zero.
- If the discriminant is greater than zero, calculate two real and distinct roots using the formula (-b + sqrt(discriminant)) / (2*a) and (-b – sqrt(discriminant)) / (2*a), and print the roots with the message “Roots are real and distinct”.
- If the discriminant is equal to zero, calculate one real and same root using the formula -b / (2*a), and print the root with the message “Roots are real and same”.
- If the discriminant is less than zero, calculate two complex and different roots using the formula (-b / 2*a) +/- (sqrt(-discriminant) / (2*a)), and print the roots with the message “Roots are complex and different”.
import math
a = 1
b = -2
c = 1
# Calculate the discriminant
discriminant = b ** 2 - 4 * a * c
if discriminant > 0:
root1 = (-b + math.sqrt(discriminant)) / (2 * a)
root2 = (-b - math.sqrt(discriminant)) / (2 * a)
print("Roots are real and distinct")
print("Root 1:", root1)
print("Root 2:", root2)
elif discriminant == 0:
root = -b / (2 * a)
print("Roots are real and same")
print("Root:", root)
else:
realPart = -b / (2 * a)
imaginaryPart = math.sqrt(-discriminant) / (2 * a)
print("Roots are complex and different")
print("Root 1:", realPart, "+", imaginaryPart, "i")
print("Root 2:", realPart, "-", imaginaryPart, "i")
Time Complexity: O(1)
Space Complexity: O(1)