C++ exercises and solutions: if else and logical operators (con.)


C++ if else and logical operators (con.)

Write a C++ program to compute the real roots of the equation: ax2+bx+c=0.


The program will prompt the user to input the values of a, b, and c. It then computes the real roots of the equation based on the following rules:
-if a and b are zero=> no solution
-if a is zero=>one root (-c/b)
-if b2-4ac is negative=>no roots
-Otherwise=> two roots
The roots can be computed using the following formula:
x1=-b+(b2-4ac)1/2/2a
x=-b-(b2-4ac)1/2/2a

Solution:


#include <cstdlib>
#include <iostream>
#include<iomanip>
#include<cmath>
 
using namespace std;
 
int main(int argc, char *argv[])
{
   float a;
   float b;
   float c;
   float delta;
 
   cout<<"Enter values of a b c separated by space:";
   cin>>a>>b>>c;
   if(a==0 && b==0)cout<<"No root";
   else if(a==0)cout<<"The equation has only one root:"<<-b/c;
   else {
        delta=b*b-4*a*c;
        if(delta<0) cout<<"No root";
        else cout<<"The equation has two roots:"<<"x="<<-b+sqrt(b*b-4*a*c)/(2*a)<<",x1="<<-b-sqrt(b*b-4*a*c)/(2*a);
        }              
  
   cout<<"\n";
   system("PAUSE");
   return EXIT_SUCCESS;
}


Comments

tedski comment

 tedski

if anyone's interest in the complex roots, I've used the following to do this exercise, but I chose to include the complex roots:
#include <math.h>
#include <iostream>
#include <cstdlib>
using namespace std;
main()
{
cout << "Quadratic equation solver by Arend Hummeling. \n\n";
float a,b,c,x1,x2;
cout << "To find the roots of the polynomial of second order, \nplease input a, b and c when the polynomial is written as: ax^2 + bx + c\n";
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c =";
cin >> c;
float d = b*b-4*a*c; //discriminant
float x1real; //for the complex roots.
float x2real;
float x1imag;
float x2imag;
int r; //number of real roots
if(d>0) r = 2;
if(d==0) r = 1;
if(d<0)
{
r = 0;
cout << "\nThe roots of this polynomial are complex numbers.";
cout << "\nWould you like to continue? (yes = 1, no =0)";
int p;
cin >> p;
if(p==0)
{
return 0;
}
if(p==1)
{
cout << "\nContinuing with the complex roots of the equation.\n";
x1real = -b/(2*a);
x1imag = pow(-d, 0.5)/(2*a);
x2real = -b/(2*a);
x2imag = -pow(-d, 0.5)/(2*a);
cout << "The first complex root of the equation has the following coordinates in the \ncomplex plane: (" << x1real << "," << x1imag << "i)\n";
cout << "The second complex root of the equation has the following coordinates in the \ncomplex plane: (" << x2real << "," << x2imag << "i)\n";
return 0;
}
}
cout << "\nThe value of the discriminant D is ." << d << "\nThis means there are "<<r<<" roots.\n";
if(r==2)
{
x1 = -b/(2*a) + pow(d, 0.5)/(2*a);
x2 = -b/(2*a) - pow(d, 0.5)/(2*a);
cout << "The first root is: " << x1 <<"\nThe second root is: "<< x2;
int tx;
tx = x1-x2;
int ty;
ty = a*(tx*tx) + b*(tx) + c;
cout << "\nThe coordinates of the top of this polynomial are: (" << tx << "" << ty << ").\n\n";
}
if(r==1)
{
x1 = x2 = -b/(2*a);
cout << "The root is: " << x1;
}
return 0;
}


2017-12-04
Afifa Adil comment

 Afifa Adil

what is delta & squirt?


2017-10-15
joshpdemesa comment

 joshpdemesa

//This code is a simpler version if you are interested.
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
double a,b,c,r1,r2;

cout<<"Enter three values for a, b and c respectively\nto compute ax^2+bx+c=0:";
cin>>a>>b>>c;

if(a==0&&b==0)
cout << "No solution!" << endl;
else if(a==0)
cout << "There is only one root: " << (-c)/b << endl;
else if((b*b)-(4*a*c)<0)
cout << "There are no roots." << endl;
else
{
r1 = -b+(sqrt((b*b)-(4*a*c)))/(2*a);
r2 = -b-(sqrt((b*b)-(4*a*c)))/(2*a);

cout<<"The first root is: " << r1 << endl;
cout<<"The second root is: " << r2 << endl;
}

system("pause");
return 0;
}


2017-09-10
Rishabh Ranga comment

 Rishabh Ranga

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
//this is a much simpler code written by me hope it helps
// feel free to point out if theres any mistake in it
void Discriminant()
{
int a,b,c,D,R;
cin >> a;
cin >> b;
cin >> c;
D = b*b-4*a*c;
R = sqrt(D);
cout << "Discriminant of your eqn is:" << R << endl;

};

void Nature()
{
int D;
if(D > 0)
cout << "Real and Unequal" << endl;
else if(D = 0)
cout << "Real and equal" << endl;
else if(D < 0)
cout << "Imaginary roots";

};


int main()
{
int a,b,c;
cout << "Enter the values of your quadtratic eqn a,b,c:\n";
Discriminant();
Nature();
getch();
}


2017-07-31
Taram Nayab comment

 Taram Nayab

What is delta and squirt?
new terms?
I dont get it


2016-12-26
m w comment

 m w

double a, b, c, d;

std::cout << "Computing the real roots of the quadratic equation ax2+bx+c." << std::endl;
std::cout << "Enter the value for a: ";
std::cin >> a;
std::cout << "Enter the value for b: ";
std::cin >> b;
std::cout << "Enter the value of c: ";
std::cin >> c;

d = b*b - 4 * a*c;

if (a == 0 && b == 0) std::cout << "No solution!" << std::endl;
else if (a == 0) std::cout << "The equation has one root : x = " << -c / b;
else if (d < 0) std::cout << "No roots!" << std::endl;
else std::cout << "The equation has two (2) roots: x1 = " << (- b - sqrt (d ))/ 2 / a << " and x2 = " << (- b + sqrt(d))/ 2 / a << std::endl;


2016-02-19
julius comment

 julius

int main() {
float a, b, c, root1, root2;
cout << "put in variables seperated by an enter" << endl;
cin >> a >> b >> c;



if (a == 0 || b == 0) {
cout << "a or b is zero";
} else if ((b * 2 - 4 * a * c ) < 0) {
cout << "doesnt have an answer";
} else if ((b * 2 - 4 * a * c ) > 0) {
root1 = (0 - b + sqrt(b * 2 - 4 * a * c )) / 2 * a;
root2 = (0 + b + sqrt(b * 2 - 4 * a * c )) / 2 * a;
cout << root1 << " " << root2 << endl;
}


}


2016-01-08



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.