Math Function Examples in C++


Math function examples in C++ are programs that use the math library functions to perform mathematical calculations. The math library provides a wide range of mathematical functions that are useful in scientific and engineering applications, as well as in everyday programming. Some examples of math function programs in C++ include:

  • Calculating the square root of a number using the sqrt() function.
  • Computing the value of a trigonometric function such as sine, cosine, or tangent using sin(), cos(), and tan() respectively.
  • Finding the maximum or minimum value of a set of numbers using fmax() and fmin().
  • Converting degrees to radians or radians to degrees using deg2rad() and rad2deg().
  • Generating a random number using rand().
  • Calculating the absolute value of a number using abs().
  • Raising a number to a power using pow().
  • Computing the logarithm of a number using log() or log10().
  • Computing the factorial of a number using a loop and the multiplication operator *.

These are just a few examples of how math functions can be used in C++ programs. There are many more functions available in the math library, and they can be combined in various ways to perform complex calculations.

Source Code Example : 1

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main()
{
    int z=-10;
    cout<<"Absolute value of z=-10 : "<<abs(z)<<endl;
    return 0;
}
To download raw file Click Here

Output

Absolute value of z=-10 : 10

Source Code Example : 2

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float x=-4.57;
    cout<<"\nAbsolute value of x=-4.57 is : "<<fabs(x)<<endl;
    return 0;
}
To download raw file Click Here

Output

Absolute value of x=-4.57 is : 4.57

Source Code Example : 3

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float y=12.3;
    cout<<"\nCeiling value of y=12.3 : "<<ceil(y)<<endl;
    return 0;
}
To download raw file Click Here

Output

Ceiling value of y=12.3 : 13

Source Code Example : 4

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float x=4.56;
    cout<<"Floor value of x=4.56 is : "<<floor(x)<<endl;
    return 0;
}
To download raw file Click Here

Output

Floor value of x=4.56 is : 4

Source Code Example : 5

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=3.0,y=4.0;
    cout<<"\nHypotenuse having other two sides as x=3.0 and"<<" y=4.0 : "<<hypot(x,y)<<endl;
    return 0;
}
To download raw file Click Here

Output

Hypotenuse having other two sides as x=3.0 and y=4.0 : 5

Source Code Example : 6

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float y=100.0;
    cout<<"\nLog value of y=100.0 is : "<<log(y)<<endl;
    return 0;
}
To download raw file Click Here

Output

Log value of y=100.0 is : 4.60517

Source Code Example : 7

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int x=3.0,y=4.0;
    cout<<"\nPower value: x^y=(3.0^4.0) : "<<pow(x, y)<<endl;
    return 0;
}
To download raw file Click Here

Output

Power value: x^y=(3.0^4.0) : 81

Source Code Example : 8

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    double y=0.25;
    cout<<"Square root value of y=0.25 : "<<sqrt(y)<<endl;
    return 0;
}
To download raw file Click Here

Output

Square root value of y=0.25 : 0.5

Source Code Example : 9

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double x=2.3;
    cout<<"\nSine value of x=2.3 : "<<sin(x)<<endl;
    cout<<"\nCosine value of x=2.3 : "<<cos(x)<<endl;
    cout<<"\nTangent value of x=2.3 : "<<tan(x)<<endl;
    x=1.0;
    cout<<"\nArc Cosine value of x=1.0 : "<<acos(x)<<endl;
    cout<<"\nArc Sine value of x=1.0 : "<<asin(x)<<endl;
    cout<<"\nArc Tangent value of x=1.0 : "<<atan(x)<<endl;
    x=57.3;
    cout<<"\nHyperbolic Cosine of x=57.3 : "<<cosh(x)<<endl;
    cout<<"\nHyperbolic tangent of x=57.3 : "<<tanh(x)<<endl;
    return 0;
}
To download raw file Click Here

Output

Sine value of x=2.3 : 0.745705

Cosine value of x=2.3 : -0.666276

Tangent value of x=2.3 : -1.11921

Arc Cosine value of x=1.0 : 0

Arc Sine value of x=1.0 : 1.5708Arc Tangent value of x=1.0 : 0.785398

Hyperbolic Cosine of x=57.3 : 3.83746e+24

Hyperbolic tangent of x=57.3 : 1


Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples