You are on page 1of 3

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

/*
Aim of the program : To find the roots of a quadratic equation
*/
/* ******************

Preprocessor directives

*********************** */

#include <iostream>
#include <cmath>
using namespace std;

// Used for cin, cout and endl


// Used for the sqrt function
// Used for cin,cout and endl functions

/* ******************

Other functions

*********************** */

// This function returns the factorial of n


double Fact(int n)
{
double x = 1;
x*=i;
for(int i = 2; i <= n; i++)
return x;
}
/* ******************

The main function

*********************** */

int main()
{
// Stores plenty of info and calculations
double a,b,c,D,x1,x2;
// Used to ask the user whether to continue
char chrCont;
cout<<"\t\t\tExperiment 1.7:

To find the roots of a quadratic equation\n\n\n";

lblBeg:
cout<<"Please enter the coefficient of x^2: ";
cin>>a;
cout<<"Please enter the coefficient of x: ";
cin>>b;
cout<<"Please enter the constant: ";
cin>>c;
cout<<"The equation is:\n"<<a<<"x^2 + "<<b<<"x + "<<c<<endl;
D = b*b - 4*a*c;
if (D > 0)
{
x1 = -b/(2*a);
x2 = sqrt(D)/(2*a);
cout<<"Its roots are real and distinct, given by "<<x1+x2<<" and "<<x1-x2;
}
else if (D==0)
{
x1 = -b/(2*a);
cout<<"Its roots are real and equal, given by "<<x1;
}
else
{
x1 = -b/(2*a);
x2 = sqrt(-D)/(2*a);
cout<<"Its roots are imaginary, given by "<<x1<<" + i"<<x2<<" and "<<x1<<" - i"<<
x2;

60
61
62
63
64
65
66
67
68
69

}
cout<<"\nDo you wish to try again? (y-Yes)";
cin>>chrCont;
if(chrCont=='y' || chrCont == 'Y') goto lblBeg;
return 0;
}

You might also like