Skip to content

C++ Program to Demonstrate math.h Library Functions

C++ Program to Demonstrate math.h Library Functions

1.Trigonometric Functions

Code:

/*Trigonometric functions*/
#include<iostream>
#include<math.h>
#define PI 3.14159265
using namespace std;
class Trigo{
double val;
public:
void getData(){
cout <<“n Enter the Valuen” << endl;
cin >>val;
}
void TrigoFun(){
cout<<endl<<“Sine                       of ” << val << ” is :” <<(val*PI/180);;
cout<<endl<<“cosine                    of ” << val << ” is :” <<cos(val*PI/180.0);
cout<<endl<<“arc tangent(atan)    of ” << val << ” is :” <<atan(val)*180/PI;
cout<<endl<<“arc tangent(aTan2) of ” << val << ” is :” <<atan2(val,-30.0)*180/PI;
cout<<endl<<“====================================================”;
}
};

int main(){
Trigo obj;
obj.getData();
obj.TrigoFun();
return 0;
}

Sample Output:

2. Exponential and logarithmic functions

Code:

/*Exponential and logarithmic functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Expo{
float val;
public:
void getData(){
cout<<“n Enter the valuen”;
cin>>val;
}
void expoResult(){
cout<<“n The exponential value of = ” << val <<exp(val);
cout<<“n Log  Value                 of = ” << val <<log(val);
cout<<“n Log2 Value                of = ” << val <<log2(val);
cout<<endl<<“====================================================”;
}
};

int main(){
Expo e;
e.getData();
e.expoResult();
return 0;
}

Sample Output:

3. Power functions

Code:

/*Power functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Power{
float val1,val2;
public:
void getData(){
cout<<“n Enter the valuen”;
cin>>val1>>val2;
}
void powFun(){
cout<<endl<<“n “<<val1<<“^” << val2 <<“= ” << pow(val1,val2);
cout<<endl<<“n Square Root of “<< val1 << ” is ” << sqrt(val1) << ” and “<< val2 <<” is ” <<sqrt(val2);
cout<<endl<<“n Cubic Root of Value1 = ” << cbrt(val1);
cout<<endl<<“n The Hypotenuse is      = ” << hypot(val1,val2);
cout<<endl<<“====================================================”;
}
};

int main(){
Power p;
p.getData();
p.powFun();
return 0;
}

Sample Output:

4. Rounding and Remainder Functions

Code:

/*Rounding and Remainder Functions*/
#include<iostream>
#include<math.h>
#include<fenv.h>
using namespace std;
class Round{
 double value;
 public:
 void getData(){
 cout<<“n Enter the valuen”;
 cin>>value;
 }
void roundResult(){
 cout<<“n Ceil Value          = “<<ceil(value);
 cout<<“n floor  Value        = “<<floor(value);
 cout<<“n trunc  Value        = “<<trunc(value);
 cout<<“n round  Value       = “<<round(value);
 cout<<“n rint  Value           = “<<rint(value);
 cout<<“n remainder  Value = “<<remainder(value,4.2);
 cout<<“n nearbyint  Value  = “<<nearbyint(value);
 cout<<endl<<“====================================================”;
}
};

int main(){
Round obj;
obj.getData();
obj.roundResult();
return 0;
}

Sample Output:

5. Minimum, Maximum, Difference Functions

Code:

/*Minimum, Maximum, Difference Functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Diff{
double val1,val2;
public:
void getData(){
cout<<“n Enter the Valuesn”;
cin>>val1>>val2;
}
void diffResult(){
cout<<“n fdim  Value of ” << val1 <<” and ” <<  val2 <<  ” is= ” << fdim(val1,val2);
cout<<“n fmax  Value of ” << val1 <<” and ” <<  val2 <<  ” is= ” << fmax(val1,val2);
cout<<“n fmin  Value of ” << val1 <<” and ” <<  val2 <<  ” is= ” << fmin(val1,val2);
cout<<endl<<“====================================================”;
}
};

int main(){
Diff d;
d.getData();
d.diffResult();
return 0;
}

Sample Output:

6. Comparison Macro Functions

Code:

/*Comparison Macro Functions*/
#include<iostream>
#include<math.h>
using namespace std;
class Comparison{
double val1,val2;
public:
void getData(){
cout<<“n Enter the Valuesn”;
cin>>val1>>val2;
}
void comparisonResult(){
cout <<“n 1 for true and 0 for false”;
cout<<“n isgreater  value     = “<<isgreater(val1,val2);
cout<<“n islessequal  Value  = “<<islessequal(val1,val2);
cout<<“n isunordered Value = “<<isunordered(val1,val2);
cout<<endl<<“====================================================”;
}
};

int main(){
Comparison obj;
obj.getData();
obj.comparisonResult();
return 0;
}

Sample Output:

Comment bellow for Query and Feedback..

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!