Program to find the Roots of a Quadratic Equation (original) (raw)

Last Updated : 20 Dec, 2025

Given a quadratic equation in the form **ax 2 + bx + c, (Only the values of **a, **b and **c are provided). We have to find its roots. If the equation has real roots, then print floor value of each root in decreasing order, If the roots are imaginary print Imaginary.

Quadratic Equations

**Examples:

**Input: a = 1, b = -2, c = 1
**Output: [ 1, 1]
**Explanation: For a = 1, b = -2, c = 1, the discriminant is 0, so the quadratic equation has one repeated real root: x =−b2 a=1 x = \frac{-b}{2a} = 1.

**Input : a = 1, b = 7, c = 12
**Output: [ -3, -4]
**Explanation: For a = 1, b = 7, c = 12, the discriminant is 49 - 48 = 1, so the roots are real and distinct: so the roots are -3 and -4.

**Input : a = 1, b = 1, c = 1
**Output : Roots are Imaginary
**Explanation: For input a = 1, b = 1, c = 1, the discriminant D=b2−4ac=1−4(1)(1)=−3D = b2 - 4ac = 1 - 4(1)(1) = -3, which is negative, so the roots are imaginary (no real roots).

Try It Yourselfredirect icon

The roots could be found using the below formula (It is known as the formula of **Sridharacharya)

x=\frac{-b\pm \sqrt{b^2-4ac}}{2a}

The values of the roots depends on the term ****(b** 2 - 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 + (b** 2 - 4ac)}/2a and ****{-b - (b** 2 - 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?-(b** 2 - 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
If a == 0
Print "imaginary" // Not a quadratic equation
Exit'
Compute d = b2 - 4ac // Discriminant
If d > 0
root1 = floor((-b + √d) / (2a))
root2 = floor((-b - √d) / (2a))
Print root1 and root2 in decreasing order
Else If d == 0
root1 = root2 = floor(-b / (2a))
Print root1 and root2
Else
Print "imaginary" // Complex roots
End

Below is the implementation of the above formula.

C++ `

#include #include
#include

using namespace std;

void findRoots(int a, int b, int c) { // If a is zero, it's not // a quadratic equation if (a == 0) { cout << "imaginary"; return; }

// Calculate discriminant D = b^2 - 4ac
int D = b * b - 4 * a * c;

// If discriminant is negative,
// roots are imaginary
if (D < 0) {
    cout << "imaginary";
    return;
}

// Compute square root of discriminant
int sqrtD = sqrt(D);

// Compute both roots using 
// quadratic formula
int r1 = floor((-b + sqrtD) / (2.0 * a));
int r2 = floor((-b - sqrtD) / (2.0 * a));

// Ensure roots are printed 
// in decreasing order
if (r1 < r2)
    swap(r1, r2);

// Print both roots
cout << r1 << " " << r2;

}

int main() { int a = 1, b = -7, c = 12; findRoots(a, b, c);

return 0;

}

C

#include <stdio.h> #include <math.h>

void findRoots(int a, int b, int c) { // If a is zero, it's not // a quadratic equation if (a == 0) { printf("imaginary"); return; }

// Calculate discriminant D = b^2 - 4ac
int D = b * b - 4 * a * c;

// If discriminant is negative,
// roots are imaginary
if (D < 0) {
    printf("imaginary");
    return;
}

// Compute square root of discriminant
int sqrtD = (int)sqrt(D);

// Compute both roots using 
// quadratic formula
int r1 = (int)floor((-b + sqrtD) / (2.0 * a));
int r2 = (int)floor((-b - sqrtD) / (2.0 * a));

// Ensure roots are printed 
// in decreasing order
if (r1 < r2) {
    int temp = r1;
    r1 = r2;
    r2 = temp;
}

// Print both roots
printf("%d %d", r1, r2);

}

int main() { int a = 1, b = -7, c = 12; findRoots(a, b, c); return 0; }

Java

import java.util.*;

public class Main { static void findRoots(int a, int b, int c) { // If a is zero, it's not // a quadratic equation if (a == 0) { System.out.print("imaginary"); return; }

    // Calculate discriminant D = b^2 - 4ac
    int D = b * b - 4 * a * c;

    // If discriminant is negative,
    // roots are imaginary
    if (D < 0) {
        System.out.print("imaginary");
        return;
    }

    // Compute square root of discriminant
    int sqrtD = (int)Math.sqrt(D);

    // Compute both roots using 
    // quadratic formula
    int r1 = (int)Math.floor((-b + sqrtD) / (2.0 * a));
    int r2 = (int)Math.floor((-b - sqrtD) / (2.0 * a));

    // Ensure roots are printed 
    // in decreasing order
    if (r1 < r2) {
        int temp = r1;
        r1 = r2;
        r2 = temp;
    }

    // Print both roots
    System.out.print(r1 + " " + r2);
}

public static void main(String[] args) {
    int a = 1, b = -7, c = 12;
    findRoots(a, b, c);
}

}

Python

import math

def findRoots(a, b, c): # If a is zero, it's not # a quadratic equation if a == 0: print("imaginary") return

# Calculate discriminant D = b^2 - 4ac
D = b * b - 4 * a * c

# If discriminant is negative,
# roots are imaginary
if D < 0:
    print("imaginary")
    return

# Compute square root of discriminant
sqrtD = math.sqrt(D)

# Compute both roots using 
# quadratic formula
r1 = math.floor((-b + sqrtD) / (2.0 * a))
r2 = math.floor((-b - sqrtD) / (2.0 * a))

# Ensure roots are printed 
# in decreasing order
if r1 < r2:
    r1, r2 = r2, r1

# Print both roots
print(r1, r2)

if name == "main": a = 1 b = -7 c = 12 findRoots(a, b, c)

C#

// 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.

JavaScript

function findRoots(a, b, c) { // If a is zero, it's not // a quadratic equation if (a === 0) { console.log("imaginary"); return; }

// Calculate discriminant D = b^2 - 4ac
let D = b * b - 4 * a * c;

// If discriminant is negative,
// roots are imaginary
if (D < 0) {
    console.log("imaginary");
    return;
}

// Compute square root of discriminant
let sqrtD = Math.sqrt(D);

// Compute both roots using 
// quadratic formula
let r1 = Math.floor((-b + sqrtD) / (2.0 * a));
let r2 = Math.floor((-b - sqrtD) / (2.0 * a));

// Ensure roots are printed 
// in decreasing order
if (r1 < r2) {
    let temp = r1;
    r1 = r2;
    r2 = temp;
}

// Print both roots
console.log(r1 + " " + r2);

}

// driver code

let a = 1, b = -7, c = 12; findRoots(a, b, c);

PHP

b,b, b,c) { // If a is zero, it's not // a quadratic equation if ($a == 0) { echo "imaginary"; return; } // Calculate discriminant D = b^2 - 4ac D=D = D=b * b−4∗b - 4 * b4a * $c; // If discriminant is negative, // roots are imaginary if ($D < 0) { echo "imaginary"; return; } // Compute square root of discriminant sqrtD=(int)sqrt(sqrtD = (int)sqrt(sqrtD=(int)sqrt(D); // Compute both roots using // quadratic formula r1=(int)floor((−r1 = (int)floor((-r1=(int)floor((b + sqrtD)/(2.0∗sqrtD) / (2.0 * sqrtD)/(2.0a)); r2=(int)floor((−r2 = (int)floor((-r2=(int)floor((b - sqrtD)/(2.0∗sqrtD) / (2.0 * sqrtD)/(2.0a)); // Ensure roots are printed // in decreasing order if ($r1 < $r2) { temp=temp = temp=r1; r1=r1 = r1=r2; r2=r2 = r2=temp; } // Print both roots echo r1."".r1 . " " . r1."".r2; } // driver code a=1;a = 1; a=1;b = -7; $c = 12; findRoots($a, b,b, b,c); ?>

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)