# C++ Program for finding the roots of Quadratic Equation

## Program to find the roots of Quadratic Equation

Here we will discuss how to find the roots of quadratic equation where it is given that real roots exists for  the given equation, using C++ programming language.

Quadratic Equation is equation of  the form ax2 + bx + c = 0, where a, b and c are real numbers and a != 0

The term `b2-4ac` is known as the discriminant of a quadratic equation. It tells the nature of the roots.

• If the discriminant is greater than `0`, the roots are real and different.
• If the discriminant is equal to `0`, the roots are real and equal.
• If the discriminant is less than `0`, the roots are complex and different.

## Algorithm:-

1. Take coefficients of quadratic equation a,b,c  as  input from the user
2. Calculate the discriminant  `Discriminant = b2-4ac`
3. if Discriminant > 0 ,
1. root1 = (-b + sqrt(Discriminant)) / (2 * a)
2. root2 = (-b – sqrt(discriminant)) / (2 * a)
4. if Discriminant == 0 ,
1. root1 =-b / (2 * a)
2. root2 = -b / (2 * a)
5. if Discriminant < 0 ,
1. root are imaginary

realPart =  -b / (2 * a)

imagPart = sqrt(-discriminant) / (2 * a)

2. roo1 = realPart+imagPart i
3. root2= realPart-imagPart i
6. Print the roots

### C++ Code:-

//C++ Program
//Program to find roots of quadratic equation
#include<iostream>
#include <math.h>
using namespace std;

int main() {
double abcdiscriminantroot1root2realPartimagPart;
cout<<“Enter coefficients a, b and c: “;
cin>>a>>b>>c;

discriminant = b * b – 4 * a * c;

// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b – sqrt(discriminant)) / (2 * a);
// to display the roots upto 2 decimal places
printf(“root1 = %.2lf and root2 = %.2lf”root1root2);
}

// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = –b / (2 * a);
printf(“root1 = root2 = %.2lf;”root1);
}

// if roots are not real
else {
realPart = –b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf(“root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi”realPartimagPartrealPartimagPart);
}

return 0;
}

Output:-
Enter coefficients a, b and c: 2.3
4
5.6
root1 = –0.87+1.30i and root2 = –0.871.30i
• Highest Common Factor(HCF): C | C++Java | Python
• Lowest Common Multiple (LCM) : C | C++ | Java | Python
• Greatest Common Divisor : C | C++ | Java | Python
• Binary to Decimal to conversion : C | C++ | Java | Python
• Binary to Octal conversion : C | C++ | Java  | Python
• Decimal to Binary conversion: C | C++ | Java | Python
• Decimal to octal Conversion: C | C++ | Java | Python
• Octal to Binary conversion : C | C++ | Java | Python
• Octal to Decimal conversion : C | C++ | Java | Python
• Quadrants in which a given coordinate lies : C | C++ | Java | Python
• Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
• Maximum number of handshakes: C | C++ | Java | Python
• Addition of two fractions: C | C++ | Java | Python
• Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
• Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
• Count possible decoding of a given digit sequence : C | C++ | Java
• Check whether a character is a vowel or consonant : C | C++ | Java | Python
• Check whether a character is a alphabet or not : C | C++ | Java | Python
• Calculate the area of a circle : C | C++ | Java | Python
• Find the ASCII value of a character : C | C++ | Java | Python
• Find the prime numbers between 1 to 100 : C | C++ | Java | Python
• Calculate the number of digits in an integer : C | C++ | Java | Python
• Convert digit/number to words : C | C++ | Java | Python
• Counting number of days in a given month of a year: C | C++ | Java | Python
• Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
• Finding number of integers which has exactly x divisors: C | C++ | Java | Python
• Finding Roots of a quadratic equation : C | C++ | Java | Python