Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Program to Solve the Quadratic Equation

C++ Program to Solve the Quadratic Equation

This program will solve quadratic equations. It accepts coefficients of a quadratic equation from the user i.e. a, b and c and displays the roots.

To compile the program name it quadratic_solver.cpp then type
g++ -o quadratic_solver quadratic_solver.cpp
You may need to use math.h like this: #include if you are using  C++ compiler software on Windows. (I tried it without the <math.h>and got an “undeclared identifier” error)

What is Quadratic Equation?

The Quadratic equation is the equation of the form as below:

ax2 + bx +c = 0

Where x represents unknown and a, b and c are coefficients, it’s roots is given by following the formula.

Quadratic Equation - C++ Implementation

Quadratic Equation – C++ Implementation

Here,

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

  1. If discriminant is greater than 0, the roots are real and different.
  2. If discriminant is equal to 0, the roots are real and equal.
  3. If discriminant is less than 0, the roots are complex and different.

C++ Program to Solve Quadratic Equation

Output of C++ Program

Compile: $ g++ -o quadratic_solver quadratic_solver.cpp

Run:

$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
6
Enter value for b:
4
Enter value for c:
1
The roots are not real numbers
x1 =-0.333333 + 0.235702 * i
x2 =-0.333333 + -0.235702 * i

Beginning C++23
Kickstart your coding journey with Beginning C++23 – the ultimate guide to mastering the latest in modern C++ programming!
View on Amazon

$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
9
Enter value for b:
24
Enter value for c:
2
The roots are:
x1 = -0.0861142 , x2 = -2.58055

About The Author

M. Saqib

Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.