In this tutorial, we will learn to find the roots or solutions of a quadratic equation in C++. In mathematics, these equations are used in fields such as simplification of expressions, equations of a circle and other conic sections, etc. Here, we will learn a method to find the roots of these equations, and a C++ program that calculates the roots of a given quadratic equation.
- C Program To Find Roots Of Quadratic Equation Using Pointers
- C Program To Find Roots Of Quadratic Equation Using Functions
- Find Quadratic Equation From Table
Quadratic equation
The general quadratic equation is as follows –
Ax^2 + Bx + C = 0
![C Program To Find Quadratic Equation C Program To Find Quadratic Equation](https://i.ytimg.com/vi/qeZIaO9Q9K4/hqdefault.jpg)
where,
- A, B, and C are known values.
A is the coefficient of the term containing x^2. Also, A cannot be 0.
B is the coefficient of the term containing x.
C is a constant value. - x is an unknown value or variable
The name ‘quadratic’ means square because the equations contain the square of the unknown variable. The quadratic equations are of degree 2.
For example –
5x^2 + 4x + 1 = 0
x^2 + 2x + 1 = 0
5x^2 + 4x + 1 = 0
x^2 + 2x + 1 = 0
C Program To Find Roots Of Quadratic Equation Using Pointers
Finding roots of a quadratic equation
For a quadratic equation ax 2 +bx+c = 0 (where a, b and c are coefficients), it's roots is given by following the formula. The term b 2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots. Step by step let’s understand how we can write a program that can be used to find the roots of a quadratic equation. First, we will ask the user for the values of a, b, and c to form the.
Every quadratic equation has exactly two roots. The roots can be equal or distinct, and real or complex. So, to find the nature of roots, calculate the discriminant using the following formula –
Undertale full game download mac. Discriminant, D = B^2 – 4AC
- Case 1 – D < 0
If D is less than 0, then the roots and distinct and complex. - Case 2 – D = 0
If D is equal to 0, then the roots are equal and real. - Case 3 – D > 0
If D is greater than 0, then both the roots are real and distinct.
To find both the roots, we use the formula given below –
Root1 = ( -B + square_root(D) ) / 2A
Root2 = ( -B – square_root(D) ) / 2A
Root2 = ( -B – square_root(D) ) / 2A
Program to find roots of a quadratic equation in C++
Now, we will see a program that calculates the roots of a quadratic equation using C++. The program takes the coefficients i.e. A, B, and C from the user and then finds the roots. The C++ program to find roots of the equation is –
In this program, we find the square root using the in-built sqrt() function of the ‘cmath’ library. The program displays both the roots of the given quadratic equation.
C++ program output
The output of the above program is –
The user has entered the value of A, B, and C as 1, 2, and -8 respectively. The roots of the equations always satisfy the equations.
For example –
For example –
So, both the roots satisfy the equation.
Thank you for reading this tutorial. I hope it helps you a lot.
- Related Questions & Answers
- Selected Reading
![C Program To Find Quadratic Equation C Program To Find Quadratic Equation](https://cdn.programiz.com/sites/tutorial2program/files/quadratic-equation-roots.jpg)
C++ProgrammingServer Side Programming
A quadratic equation is in the form ax2 + bx + c. The roots of the quadratic equation are given by the following formula −
There are three cases −
b2 < 4*a*c - The roots are not real i.e. they are complex
b2 = 4*a*c - The roots are real and both roots are the same.
b2 > 4*a*c - The roots are real and both roots are different
The program to find the roots of a quadratic equation is given as follows.
Example
C Program To Find Roots Of Quadratic Equation Using Functions
Output
In the above program, first the discriminant is calculated. If it is greater than 0, then both the roots are real and different.
This is demonstrated by the following code snippet.
If the discriminant is equal to 0, then both the roots are real and same. This is demonstrated by the following code snippet.
Find Quadratic Equation From Table
If the discriminant is less than 0, then both the roots are complex and different. This is demonstrated by the following code snippet.