**Roots of a Quadratic Equation in C:** The roots/Solutions of the quadratic equation **ax ^{2} + bx + c = 0** are the values of the variable

**x**that satisfy the equation. This means finding the value of

**x**such that the whole equation sums to 0. Because the degree (*HIGHEST POWER OF A VARIABLE) of a quadratic equation is 2, it can only have two roots. Roots can be

**real**and

**imaginary**.

Write a C program that finds the roots of a quadratic equation and displays the type of root (real and unique or 2 complex roots or 2 equal roots).

1. Take **a**, **b** and **c** value as input.

2. First it finds discriminant using the formula : **disc = b * b – 4 * a * c**.

3. After finding the discriminant, find the nature of the roots, this will tell us which operations to perform to find the roots of the quadratic equation.

4. There are 3 types of roots. They are complex, distinct & equal roots. We have to find the given equation belongs to which type of root.

- If
**discriminant > 0**, the roots are real and distinct. - If
**discriminant = 0**, the roots are real and equal. - If
**discriminant < 0**, the roots are real and imaginary.

- If
**discriminant = 0**, then both roots are real and equal.Root1 == Root2 =

**-b/2a** - If
**discriminant > 0**, then roots are real and distinct.**Root1**= [(- b)+sqrt(d)]/2a

**Root2**= [(- b)-sqrt(d)]/2a - If
**discriminant < 0**, then roots are real and imaginary.**real part**= (-b)/2*a;**imaginary part**= [i * √d]/(2*a)Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!advertisementadvertisement**Root1**= Real part + imaginary part

**Root2**= Real part – imaginary part

Here is source code of the C program to find the roots of a quadratic equation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C Program to find the roots of a quadratic equation and their nature. */ #include<stdio.h> #include<math.h> //Header file is include as we use sqrt function. int main() { /* Quadratic Equation is in the form a*x^2 + b*x + c = 0 a=>coefficient of x^2 b=>coefficient of x c=>constant */ float a,b,c; float d; //discriminant of the equation. float root1,root2; float imaginary_part; //for instances where discriminant < 0 printf("The equation is in the form of a*x^2 + b*x + c = 0 \n"); printf("Enter the coefficient of term x^2 i.e a = "); scanf("%f",&a); printf("\n"); printf("Enter the coefficient of term x i.e b = "); scanf("%f",&b); printf("\n"); printf("Enter the constant term i.e c = "); scanf("%f",&c); printf("\n"); if(a==0) { printf("The equation given is not a quadratic equation \n"); return 0; } //Value of discriminant is found here. d=(b*b)-(4.0*a*c); //Printing the discriminant of the equation. printf("Discriminant of the equation: %f",d); printf("\n"); if(d>0) { printf("Roots are real and unique\n"); root1=b*(-1.0); //finding the first root. //As we are operating on floating point number, so have written -1.0 root1=root1+sqrt(d); root1=root1/(2.0*a); root2=b*(-1.0); //finding the second root. root2=root2-sqrt(d); root2=root2/(2.0*a); printf("root 1= %f \n root 2= %f",root1,root2); } if(d==0) { root1=((b*-1.0)/(2.0*a)); root2=root1; printf("Root are real and equal to each other\n"); printf("root 1= %f \n root 2= %f",root1,root2); } if(d<0) { imaginary_part=sqrt(-1.0*d)/(2.0*a); root1=(-1.0*b)/(2.0*a); root2=root1; //root1's real part is equal to root2's real part also the imaginary part //but one is summation of both parts and one is diffrence of both parts printf("Roots are imaginary \n"); printf("Root 1= %f+%f i \t \nRoot 2= %f-%f i",root1,imaginary_part,root2,imaginary_part); } return 0; }

1. First, we enter the coefficient values of each term in the quadratic equation in the form of **ax ^{2} + bx + c = 0** and store the values using ‘

**a**’, ’

**b**’ and ‘

**c**’ variables.

2. If a==0, the equation cannot be a quadratic equation since a quadratic equation has degree=2, which is the highest power of a variable, but if a=0, the term x

^{2}is 0 and the equation becomes a linear equation. In that case, we simply terminate the program with a return statement. Otherwise, we’ll find the discriminant.

3. Find the discriminant value using the formula :

**disc = b * b – 4 * a * c**.

4. If

**discriminant > 0**, find both the roots via formulas expressed in the program i.e.

**Root1**= [(- b)+sqrt(d)]/2a

**Root2**= [(- b)-sqrt(d)]/2a

Then, print roots are real and distinct.

5. If

**discriminant = 0**, then just find

**root1=root2= -b/2*a**and print both roots are real and equal.

6. If the

**discriminant < 0**, then find the real part and then find the imaginary part, first print the difference between real part and imaginary part then print the summation of real and imaginary part.

**Example 1:**

Given quadratic equation is X^{2}+4x-5 = 0. Here, a = 1, b = 4, c = -5

discriminant **(disc) = b * b – 4 * a * c** = (4 * 4) – (4 * 1 * -5) = 36

Since, discriminant > 0, then there will be two roots unique to each other and real.

Value of **Root1 = [(- b)+sqrt(d)]/2a** => [(-4)+sqrt(36)]/2*1 => (-4+6)/2 => 1

Value of **Root2 = [(- b)+sqrt(d)]/2a** => [(-4)-sqrt(36)]/2*1 => (-4-6)/2 => -5

**Example 2:**

Given quadratic equation is X^{2}-2x+1 = 0. Here, a = 1, b = -2, c = 1

discriminant **(disc) = b * b – 4 * a * c** = (-2 * -2) – (4 * 1 * 1) = 0

Since, discriminant = 0, then both roots are real and equal.

Value of **Root 1 = -b/(2*a)** = 2/(2*1) = 1.000000

Root1 = 1.000000

As when Determinant == 0, roots are equal hence, root1=root2=1.000000.

**Example 3:**

Given quadratic equation is X^{2}+4x+16 = 0. Here, a = 1, b = 4, c = 16

discriminant **(disc) = b * b – 4 * a * c** = (4 * 4) – (4 * 1 * 16) = -48

Since, discriminant < 0, then roots are real and imaginary.

**real part** = (-b)/2*a = (-4)/2*1 = -2.000000

**imaginary part** = [i * √d]/(2*a) = [i * √36]/2*1 = 3.464102 i

**Root1** = Real part + imaginary part = -2.000000 + 3.464102 i

**Root2** = Real part – imaginary part = -2.000000 – 3.464102 i

**Time Complexity: O(log(n))**

The above program to find the root of a quadratic equation takes O(log(n)) times because the sqrt() function finds the square root of the function and is said to be one of the most efficient binary search algorithms used in its backend, based on a divide-and-conquer approach. The time complexity of binary search is O(log(n)), so we can conclude that the time complexity is O(log(n)), where n = number whose square root is to be found.

**Space Complexity: O(1)**

In this program, we are not initializing any array or other data types that takes lot of storage. We are just initializing the auxiliary variables. Therefore, our space complexity is constant, i.e., O(1).

**Testcase 1:** In this case, we check if the roots are imaginary.

The equation is in the form of a*x^2 + b*x + c = 0 Enter the coefficient of term x^2 i.e a = 1 Enter the coefficient of term x i.e b = 4 Enter the constant term i.e c = 16 Discriminant of the equation: -48.000000 Roots are imaginary Root 1 = -2.000000+ 3.464102 i Root 2 = -2.000000- 3.464102 i

**Testcase 2:** In this case, we check if the roots of a quadratic equation are real and unique.

The equation is in the form of a*x^2 + b*x + c = 0 Enter the coefficient of term x^2 i.e a = 1 Enter the coefficient of term x i.e b = 4 Enter the constant term i.e c = -5 Discriminant of the equation: 36.000000 Roots are real and unique root 1 = 1.000000 root 2 = -5.000000

**Testcase 3:** In this case, we check if the roots are real and equal.

The equation is in the form of a*x^2 + b*x + c = 0 Enter the coefficient of term x^2 i.e a = 1 Enter the coefficient of term x i.e b = -2 Enter the constant term i.e c = 1 Discriminant of the equation: 0.000000 Root are real and equal to each other root 1 = 1.000000 root 2 = 1.000000

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy C Books
- Apply for C Internship
- Apply for Computer Science Internship
- Practice BCA MCQs
- Practice Computer Science MCQs