C++ | roots of quadratic equations.
C++ | Roots Of Quadratic Equations.
Hello peeps! Lord Hypersonic greets you. The c++ program I am presenting today can solve any quadratic equations. The full explanation of the program is given below.
Header files used:-
- iostream: For using input and output stream (cout for output stream or for printing output and cin for input stream or for taking input from the user) and to use fixed function (It is used to sets the floatingfield format flag for the str stream to fixed. When floating filed is set to fixed, floating-point values are written fixed point notation: the value is represented with exactly as many digits in the decimal part as specified by the precision field and with no exponent parts).
- math.h: For using sqrt() function to find the square root of D (which is equal to b2 - 4ac).
- iomanip: For using setprecision() function (this function is used to sets the decimal precision to be used to format floating point values on output operation, for example, if the number is 3.14158 and we use this function as setprecision(3), the output will be 3.14).
- conio.h: For using getch() function (this function is used to take input from the user as a single character).
Variables used:-
- a of double data-type.
- b of double data-type.
- c of double data-type.
- x1 of double data-type.
- x2 of double data-type.
- D of double data-type.
Working:-
- First, it will ask the user to enter values of a,b and c from the equation represented as ax2+bx+c=0. The values provided by the user for a,b and c will be stored in variables a, b and c.
- After obtaining values of a, b and c from the user it calculates the value of variable D (also known as discriminant) with the formula b2 - 4ac.
- After calculating D, it will check that if the value of D is smaller than 0 or is greater than equal to 0.
- If D is smaller than zero then there will be no real roots of the equation and hence it will print "Roots are not real".
- If D is greater than equal to zero then roots of the equation are real and are calculated by the formula: x= (-b ± √D)/2a
- Using the above formula, there will be two answers. So to store two answers we need two variables x1 and x2.
- In x1 we use the formula x1= (-b + √D)/2a and in x2 we use the formula x= (-b - √D)/2a, so we have two roots of the equation x1 and x2.
- Now it's time to print the roots. For printing, we use the fixed function to set floatingfield format flag. Then we use the function setprecision() to print the roots obtained up to two decimal point. For example: if one of the root is 4.2341234213, we can not show the whole number as root, so we first use fixed function so that setprecision() function can read the number from the decimal. So we have to print the number up to two decimal number, so we have to write setprecision(2) then the number which is 4.2341234214. The output we obtain is 4.23. So for printing roots, we will write:
cout<<fixed;
cout<<setprecision(2)<<x1<<setprecision(2)<<x2; - The above statement will print the roots up to 2 decimal units.
Source Code:-
/*
*******************************************************
* Program: Solve quadratic equation *
* Description: This program can solve any quadratic equation. *
* Author: Lord Hypersonic *
* website: www.lordhypersonic.blogspot.com *
* *
*******************************************************
*/
#include <iostream>
#include <math.h>
#include <iomanip>
#include <conio.h>
using namespace std;
int main()
{
double a,b,x1,x2,c,D;
for(;;)
{
cout<<"Enter values of a, b and c from equation (ax^2+bx+c=0): ";
cin>>a>>b>>c; //asking for values of a,b and c from expression ax^2+bx+c=0
D=(b*b)-(4*a*c); //calculating Discriminant.
if(D<0) // If D is negative then roots of equation are imaginary.
{
cout<<"Roots are not real\n\n";
}
else //If D is positive then roots of equation are real and then we will solve it further
{
x1=((-b+sqrt(D))/(2*a)); x2=((-b-sqrt(D))/(2*a)); //formula for calculating roots of equation
cout<<fixed;
cout<<"Roots of equation are: "<<setprecision(2)<<x1<<" and "<<setprecision(2)<<x2<<endl<<endl; //printing the roots of equation.
}
}
cout<<"\nThank you\n";
getch(); //waiting for user to press any key to exit the program.
return 0;
}
Here is the video of working of the program.*******************************************************
* Program: Solve quadratic equation *
* Description: This program can solve any quadratic equation. *
* Author: Lord Hypersonic *
* website: www.lordhypersonic.blogspot.com *
* *
*******************************************************
*/
#include <iostream>
#include <math.h>
#include <iomanip>
#include <conio.h>
using namespace std;
int main()
{
double a,b,x1,x2,c,D;
for(;;)
{
cout<<"Enter values of a, b and c from equation (ax^2+bx+c=0): ";
cin>>a>>b>>c; //asking for values of a,b and c from expression ax^2+bx+c=0
D=(b*b)-(4*a*c); //calculating Discriminant.
if(D<0) // If D is negative then roots of equation are imaginary.
{
cout<<"Roots are not real\n\n";
}
else //If D is positive then roots of equation are real and then we will solve it further
{
x1=((-b+sqrt(D))/(2*a)); x2=((-b-sqrt(D))/(2*a)); //formula for calculating roots of equation
cout<<fixed;
cout<<"Roots of equation are: "<<setprecision(2)<<x1<<" and "<<setprecision(2)<<x2<<endl<<endl; //printing the roots of equation.
}
}
cout<<"\nThank you\n";
getch(); //waiting for user to press any key to exit the program.
return 0;
}
Post a Comment