Mathematical functions
Required header: #include <cmath>
sqrt(x) | square root |
sin(x) | sine of x (in radians) |
asin(x) | Inverse Sine of x |
cos(x) | cosine of x (in radians) |
acos() | Inverse cosine of x |
tan(x) | tangent of x (in radians) |
atan() | Returns Inverse tangent a Number |
abs() | returns absolute value of an argument |
log(x) | natural logarithm of x (base e) |
log10(x) | logarithm of x to base 10 |
exp(x) | exponential function |
pow(x,y) | x raised to power y |
cbrt() | Computes Cube Root of a Number |
1.Program to calculate roots of a quadratic equation
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
float a,b,c,d,real,imag;
cout<<"enter a,b,c"<<endl;
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
cout<<"The given equation is"<<a<<"x^2+"<<b <<"x+"<<c <<endl;
cout<<"The discriminant is" << d <<endl;
if(d>0)
{cout<<"The given equation has two real distinct roots"<<endl;
cout<<"first root is x=" <<(-b+sqrt(d))/(2*a);
cout<<"second root is x=" <<(-b-sqrt(d))/(2*a);
}
if(d==0)
{cout<<"The given equation has two same roots"<<endl;
cout<<"it has two same roots equal to x= " <<-b/(2*a);
}
if (d<0)
{cout<<"The given equation has two imaginary roots"<<endl;
real= -b/(2*a);
imag=(sqrt(-d))/(2*a);
cout<<"first root is x=" <<real <<"+"<< imag<<"i";
cout<<"second root is x=" <<real <<"+"<< imag<<"i";
}
return 0 ;
}
No comments:
Post a Comment