100% found this document useful (1 vote)
288 views13 pages

Cmath Header File

The document provides examples of various math functions from the C++ math.h header file, including trigonometric, inverse trigonometric, exponential, logarithmic, and other functions. For each function, it gives the syntax, describes what the function calculates, and provides a short code sample to demonstrate how to use the function. The examples are intended to help programmers learn how to properly implement the math functions in their C++ programs.

Uploaded by

Code Seekers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
288 views13 pages

Cmath Header File

The document provides examples of various math functions from the C++ math.h header file, including trigonometric, inverse trigonometric, exponential, logarithmic, and other functions. For each function, it gives the syntax, describes what the function calculates, and provides a short code sample to demonstrate how to use the function. The examples are intended to help programmers learn how to properly implement the math functions in their C++ programs.

Uploaded by

Code Seekers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

[email protected]. https://programcodescpp.wixsite.

com/programcodes

ALL FUNCTIONS
OF math.h
HEADER FILE
WITH EXAMPLE
IN C++

By DJVprogrammers

https://web.facebook.com/DJVprogrammers 1
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF COS-1 , SIN-1 , TAN-1 builtin function in C++


• Syntax
o Double acos(double);
o Double asin(double);
o Double atan(double);
o Return type and parameters are double.. but it returns answer in radians so
multply it by (180.0/pi) to convert it in degrees

Code:
1. #include <iostream>
2. #include <math.h>
3. using namespace std;
4. #define PI 3.14159
5. int main ()
6. {
7. double x, ans;

8. cout<<"\n\tEnter Value to find Its cos , sin, tan inverse : ";


9. cin>>x;
10. cout<<"\n\tCos Inverse = "<< acos(x) * 180.0/PI<<endl;
11. cout<<"\n\tSin Inverse = "<< asin(x) * 180.0/PI<<endl;
12. cout<<"\n\tTan Inverse = "<< atan(x) * 180.0/PI<<endl;

13. return(0);
14. }

https://web.facebook.com/DJVprogrammers 2
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF COS , SIN, TAN builtin function in C++


• Syntax
o Double cos(double);
o Double sin(double);
o Double tan(double);
o Return type and parameters are double.. but it takes angle in radians so multiply
your answer by (pi/180) to convert it in radians

Code:
1. #include <iostream>
2. #include <math.h>
3. using namespace std;
4. #define PI 3.14159
5. int main ()
6. {
7. double x;
8. cout<<"\n\tEnter Angle to find cos , sin, tan : ";
9. cin>>x;
10. cout<<"\n\tCos"<<x<<" = "<< cos(x * PI/180.0) <<endl;
11. cout<<"\n\tSin"<<x<<" = "<< sin(x * PI/180.0)<<endl;
12. cout<<"\n\tTan"<<x<<" = "<< tan(x * PI/180.0)<<endl;
13. return(0);
14. }

https://web.facebook.com/DJVprogrammers 3
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF coshx , sinhx, tanhx builtin function in C++


• Syntax
o Double cosh(double);
o Double sinh(double);
o Double tanh(double);
o Return type and parameters are double..

Code:
1. #include <iostream>
2. using namespace std;
3. #include <math.h>
4. int main () {
5. double x;
6. cout<<"\n\tEnter value of x to find coshx, sinhx, tanhx : ";
7. cin>>x;
8. //coshx hyperbolic cos of x
9. cout<<"\n\tThe hyperbolic cosine of "<<x<<" = "<<cosh(x);
10. //sinhx hyperbolic sin of x
11. cout<<"\n\n\tThe hyperbolic sine of "<<x<<" = "<<sinh(x);
12. //tanhx hyperbolic tan of x
13. cout<<"\n\n\tThe hyperbolic tangent of"<<x<<" = "<<tanh(x);
14. return 0;
15. }

https://web.facebook.com/DJVprogrammers 4
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF EXP FUNCTION OF CMATH LIBRARY

• SYNTAX
o Double exp(double x);
o This function double exp(double x) returns the value of e raised to the xth
power.

CODE:
1. #include <iostream>
2. using namespace std;
3. #include <math.h>
4. int main () {
a. double x;
b. cout<<"\n\tEnter power x : ";
c. cin>>x;
d. cout<<"\n\tThe exponential value of "<< x<<" = "<<exp(x)<<endl;
5. return(0);
6. }

https://web.facebook.com/DJVprogrammers 5
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF LOGARITHMIC FUNCTION OF CMATH


LIBRARY

• SYNTAX
o double log(double x)
o this function double log(double x) returns the natural logarithm (base-e
logarithm) of x.
o double log10(double x)
o This function double log10(double x) returns the common logarithm (base-10
logarithm) of x.

CODE:
1. #include <iostream>
2. using namespace std;
3. #include <math.h>
4. int main () {
a. double x;
b. cout<<"\n\tEnter x : ";
c. cin>>x;
d. cout<<"\n\tln ( "<< x<<" ) = "<<log(x)<<endl;
e. cout<<"\n\tlog ( "<< x<<" ) = "<<log10(x)<<endl;
5. return(0);
6. }

https://web.facebook.com/DJVprogrammers 6
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF MODF FUNCTION IN CMATH


• SYNTAX:
• The C library function double modf(double x, double *integer) returns the fraction
component (part after the decimal), and sets integer to the integer component.
• double modf(double x, double *integer)
• x − This is the floating point value.
• integer − This is the pointer to an object where the integral part is to be stored.

CODE:

1. #include <iostream>
2. using namespace std;
3. #include<math.h>
4. int main () {
5. double x, fpart, intpart;
a. cout<<"\n\tEnter x : ";
b. cin>>x;
6. fpart = modf(x, &intpart);
7. cout<<"\n\tIntegral part = "<< intpart<<endl;
8. cout<<"\n\tFraction Part = "<< fpart<<endl;
9. return(0);
10. }
https://web.facebook.com/DJVprogrammers 7
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF POWER FUNCTION IN C++


• SYNTAX:
o The C library function double pow(double x, double y) returns x raised to the
power of y i.e. xy .

CODE:
1. #include <iostream>
2. using namespace std;
3. #include<math.h>
4. int main () {
5. double x,y;
a. cout<<"\n\tEnter x : ";
b. cin>>x;
c. cout<<"\n\tEnter y : ";
d. cin>>y;
e. cout<<"\n\t "<<x<<" ^ "<<y<<" = "<<pow(x,y)<<endl;
6. return 0;
7. }

https://web.facebook.com/DJVprogrammers 8
[email protected]. https://programcodescpp.wixsite.com/programcodes

EXAMPLE OF SQRT FUNCTION IN C++


• SYNTAX:
o The C library function double sqrt(double x) returns the square root of x.

CODE:

1. #include <iostream>
2. using namespace std;
3. #include<math.h>

4. int main () {
5. double x;
6. cout<<"\n\tEnter x : ";
7. cin>>x;
8. cout<<"\n\t "<<char(251)<<x<<" = "<<sqrt(x)<<endl;

https://web.facebook.com/DJVprogrammers 9
[email protected]. https://programcodescpp.wixsite.com/programcodes

9. return(0);
10. }

EXAMPLE OF CEIL and FLOOR FUNCTION IN C++


• SYNTAX:
o This function double ceil(double x) returns the smallest integer value greater than
or equal to x.
o This function double floor(double x) returns the largest integer value less than or
equal to x.

CODE:

1. #include <iostream>
2. using namespace std;
3. #include<math.h>
4. int main ()
5. {
6. double x;
a. cout<<"\n\tEnter x : ";
b. cin>>x;
https://web.facebook.com/DJVprogrammers 10
[email protected]. https://programcodescpp.wixsite.com/programcodes

c. cout<<"\n\t smaller integer value nearest to "<<x<<" = "<<floor(x)<<endl;


d. cout<<"\n\t larger integer value nearest to "<<x<<" = "<<ceil(x)<<endl;
7. return(0);
8. }

EXAMPLE OF FMOD FUNCTION IN C++


• SYNTAX:
o The C library function double fmod(double x, double y) returns the remainder of
x divided by y.

CODE:

1. #include <iostream>
2. using namespace std;
3. #include<math.h>
4. int main ()
5. {
6. double x,y;
a. cout<<"\n\tEnter x : ";
b. cin>>x;
c. cout<<"\n\tEnter y : ";
https://web.facebook.com/DJVprogrammers 11
[email protected]. https://programcodescpp.wixsite.com/programcodes

d. cin>>y;
e. cout<<"\n\tModulus of "<<x<<" / "<<y<<" = "<<fmod(x,y)<<endl;
7. return(0);
8. }

EXAMPLE OF ABS/FABS FUNCTION IN C++


• SYNTAX:
o The C library function double fabs(double x,) returns the absolute value of x

CODE:

1. #include <iostream>
2. using namespace std;
3. #include<math.h>

4. int main ()
5. {
6. double x;
a. cout<<"\n\tEnter x : ";

https://web.facebook.com/DJVprogrammers 12
[email protected]. https://programcodescpp.wixsite.com/programcodes

b. cin>>x;
c. cout<<"\n\tAbsolute of "<<x<<" = "<<fabs(x)<<endl;
7. return(0);
8. }

Website:
https://programcodescpp.wixsite.com/programcodes

Email:
[email protected]

Facebook Page:
https://web.facebook.com/DJVprogrammers

YouTube Channel
https://www.youtube.com/channel/UCfizosx-0fkFJ6R-oF6l9-A?view_as=subscriber

https://web.facebook.com/DJVprogrammers 13

You might also like