Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are general Quadratic Equations?
3.
Nature of Roots
4.
Steps to Find the Square Roots of the Quadratic Equation
5.
Design (Algorithm)
6.
Code for Quadratic Equation Program in C
6.1.
Time Complexity
6.2.
Space Complexity
7.
Analysis
8.
Frequently Asked Questions
8.1.
Q. What is the C quadratic equation?
8.2.
Q. What is another name for quadratic?
8.3.
Q. What are some real-life applications of quadratic equations?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Find the Roots of a Quadratic Equation in Program C

Author Nitika
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this article, we will learn about quadratic equations, nature of quadratic equations and perform Quadratic Equation Program in C language

Quadratic equation roots program

What are general Quadratic Equations?

Quadratic equations play a significant role in mathematics and various scientific applications. They represent second-degree polynomial equations commonly encountered in physics, engineering, and other fields. 

Quadratic equations are polynomial equations with a degree of 2. They are expressed as:

ax² + bx +c = 0, 

where, a ≠ 0

aand are the coefficient variables of the equation. 

The value of x is used to find the roots of the quadratic equation (a, b). 

In a quadratic equation roots are expressed as:

x = (-b ± (√b2-4ac))/2a

According to this the two roots of the quadratic equation will be 

 (-b -√D)/2a  and (-b+ √D)/2a

Here, 

'b²-4ac' is called the Discriminant(D)

 

The roots of a quadratic equation are expressed in three ways: 

  1. Real & distinct
     
  2. Real & equal
     
  3. Real & imaginary

The nature of the equation is decided by the Discriminant.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Nature of Roots

The discriminant D = b²-4ac, nature of roots are defined as:

If Discriminant > 0 : Real and Distinct,

If Discriminant = 0 : Real and Equal,

If Discriminant < 0 : Real and Imaginary

 

The above table explains that if the Discriminant is greater than 0, the roots will be real & distinct (unequal); if the Discriminant is equal to 0, the roots will be real & equal; lastly, when the Discriminant is less than 0, the roots will be real & imaginary.

Click on the following link to read further: Features of C Language.

Also see, C Static Function

Steps to Find the Square Roots of the Quadratic Equation

The following is the algorithm for Quadratic Equation Program in C.
 

  • Declare all the variables used in the equation at the start and take input.

 

  • Find the value of the Discriminant( b²- 4ac ) of the equation and then proceed to find the nature of the quadratic equation roots.

 

  • If the value of the Discriminant is greater than 1, print the roots come out to be real & distinct.

 

  • If the value of Discriminant is equal to 1, print that both roots come out to be real & equal.

 

  • If the value of Discriminant is less than 1, print that both the roots come out to be imaginary.


Stop the program.

Design (Algorithm)

We will design an algorithm to calculate the roots of a quadratic equation. Firstly we will take the three coefficients, i.e., a,b, and c in ax2 + bx + c = 0, as input from the user. Now to determine the nature of roots, we will calculate the value of the discriminant using the formula  b*b - 4 *a*c. Using the if-else conditional statement, if the condition discriminant(D) is greater than zero is true, then the two roots are distinct and real, so we can calculate them using the formula r1 = (-b + sqrt(D)) / (2 * a) and r2 = (-b - sqrt(D)) / (2 * a). If the discriminant is equal to zero, the roots are real and equal, and we will calculate the roots using the formula r1 = -b / (2 * a). If the discriminant is less than zero, then the roots are imaginary. Here the algorithm ends. 

The following image shows a flowchart of the above algorithm.

Flowchart

Code for Quadratic Equation Program in C

Let’s implement quadratic equation program in c :

 

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


int main()
{
    float a, b, c, r1, r2, D;
    printf("Enter 3 values: ");
    scanf("%f %f %f", &a, &b, &c);


//Formula for calculating Discriminant.
	
    D = b * b - 4 * a * c;


    if (D > 0)
    {
    	//real & different roots.
    	
        r1 = (-b + sqrt(D)) / (2 * a);
        r2 = (-b - sqrt(D)) / (2 * a);
        printf("ROOT 1 IS = %f\n", r1); 
        printf("ROOT 2 IS = %f", r2); 
    }
    else if (D == 0)
    {
    	
    	//real & equal roots.
    	
        r1 = -b / (2 * a);
        r2 = r1; 
        printf("ROOT 1 IS = %f\n", r1); 
        printf("ROOT 2 IS = %f", r2); 
    }
    else
    {
        printf("REAL ROOTS DO NOT EXIST\n"); 
    }


    return 0; 
}

Sqrt() is used to find the square root of a number.

Output 

Enter 3 values: 5 80 125
ROOT 1 IS = -1.755002
ROOT 2 IS = -14.244998

 

Enter 3 values: 1 3 4
REAL ROOTS DO NOT EXIST

Time Complexity

O(1), Here all the computations are done in a constant time hence the time complexity is O(1).

Space Complexity

O(1), Here we are not using any extra space hence the space complexity is  O(1)

Analysis

As we discussed, the different nature of roots is based on the discriminant's value. The discriminant (D) of a quadratic equation ax^2 + bx + c = 0 determines the nature of its roots. 'b²-4ac' is called the Discriminant(D). Let us analyze the nature of roots with examples and formulas.

  • If D > 0, the equation has two distinct real roots.
    For Example, for the quadratic equation 2x^2 - 5x + 3 = 0, the value of discriminant is, D = (-5)^2 - 4(2)(3) = 1. The Discriminant(D) is greater than 0 and has two distinct real roots. We will calculate roots using the formula x = (-b ± (√D))/2a. So the two roots are x = (-(-5) ± (1))/2(2)), i.e. x = 1 and x = 1.5.
     
  • If D = 0, the equation has two equal real roots.
    For Example, for the quadratic equation x^2 - 4x + 4 = 0, the value of discriminant is, D = (-4)^2 - 4(1)(4) = 0. The Discriminant(D) is equal to 0, so it has two equal real roots. We will calculate the unique root using the formula x = (-b ± (√D))/2a. So the value of root is x = 2 x = (-(-4) ± (0))/2(1)), i.e. x = 2.
     

If D < 0, the equation has no real roots but two complex conjugate roots.
For Example, for the quadratic equation 3x^2 + 2x + 5 = 0, the value of discriminant is, D = 2^2 - 4(3)(5) = -56. As the value of Discriminat is less than zero, there are no real roots for the given equation.

Also read about Tribonacci Series and Short int in C Programming

Frequently Asked Questions

Q. What is the C quadratic equation?

A C quadratic equation is a mathematical expression in the form "ax^2 + bx + c = 0," where a, b, and c are coefficients. It's used to find the solutions (roots) for the variable x.

Q. What is another name for quadratic?

Another name for quadratic is second degree. Quadratic equations involve terms with exponents raised to the power of two (x^2), which is the second degree in algebraic equations.

Q. What are some real-life applications of quadratic equations?

Real-life applications of quadratic equations include calculating areas, speed, projectile motion, trajectories, profit & loss etc. it also has its uses in the economics sector, where it is used for optimizing costs and maximizing profits. Its also used in satellites and signal transmission.

Conclusion

In this article, we explored various things related to Quadratic Equation Program in C, leaving no stone unturned. We studied general quadratic equations, roots of the equation, nature of the quadratic equation, steps, algorithm and flowchart of the Quadratic Equation Program in C. 
 

We believe this article on how to become a data scientist in India was helpful.
 

Refer to our guided paths on Coding Ninjas Studio to learn more about Data Science Fundamentals, Python, DSA, etc. Enrol in our courses at Coding Ninja and refer to the problems available and mock tests. Take a look at the interview bundle and interview Experiences of popular companies.

Happy Learning Ninja!

Previous article
C Program for Tower of Hanoi
Next article
Difference Between C and C++
Live masterclass