Author Topic: find roots of a quadratic equation using C++  (Read 1872 times)

Offline sidhantverma_nsr

  • STP Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
  • Rating : +0/-0
find roots of a quadratic equation using C++
« on: May 12, 2010, 07:09 AM »
using this program one can put any quadratic equation and can get roots of it..

   

Offline fiedel

  • STP Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
  • Rating : +1/-0
Re: find roots of a quadratic equation using C++
« Reply #1 on: July 12, 2011, 01:24 AM »
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
// Variable Declerations
double a, b, c;

//Variable Inputs
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;

//Computations
double discriminant = (pow(b,2) - 4*a*c);
double positive_root = (((-b) + sqrt(discriminant))/(2*a));
double negative_root = (((-b) - sqrt(discriminant))/(2*a));

//Output
if (discriminant == 0)
{
cout << "\n\nThe discriminant is ";
cout << discriminant << endl;
cout << "The equation has a single root.\n\n";
}
else if (discriminant < 0)
{
cout << "\n\nThe discriminant is ";
cout << discriminant << endl;
cout << "The equation has two complex roots.\n\n";
}
else
{
cout << "\n\nThe discriminant is ";
cout << discriminant << endl;
cout << "The equation has two real roots.\n\n";
}

//Final Root Values
cout << "The roots of the quadratic equation are x = ";
cout << negative_root;
cout << ", ";
cout << positive_root << endl << endl;

return 0;


}

Tags:
 

Related Topics

  Subject / Started by Replies Last post
3 Replies
1241 Views
Last post February 23, 2012, 05:45 AM
by lello
1 Replies
1150 Views
Last post April 29, 2008, 01:10 PM
by gkunalin
0 Replies
1363 Views
Last post July 24, 2009, 12:43 PM
by nishunu
2 Replies
1729 Views
Last post November 12, 2011, 10:46 AM
by twente
3 Replies
2990 Views
Last post October 20, 2009, 09:50 AM
by anurag08
22 Replies
10831 Views
Last post March 21, 2010, 10:20 PM
by kiatz15
1 Replies
3181 Views
Last post February 18, 2012, 08:28 AM
by FireWings

Jokes on Sardar