Saturday, May 23, 2020

Math function in c programming with examples

Note : - At first define math header file in a c program to access the all math function in c programming for example (sin(),cos(),tan(),pow(),sqrt()) and many more.

Math function in c programming with examples
Math function in c programming with examples

acos() function in c programming language : -

asoc () function in c programming helps to return the arc cosin value of element(x).

Synatx : -  

double acos(double x);

asoc () function in c will return the value between 0 and π.

 asoc() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the acos of */
    value = 0.5;

    /* Calculate the Arc Cosine of value */
    result = acos(value);

    /* Display the result of the calculation */
    printf("The Arc Cosine of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Cosine of 0.500000 is 1.047198

asin() function in c programming language : -

asin() function in c do the work same as asin() it helps you to return the arc sine value of element(x).

Synatx : - 

double asin(double x); 

asoc () function in c will return the value between -π/2 and π/2.

asin() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the asin of */
    value = 0.5;

    /* Calculate the Arc Sine of value */
    result = asin(value);

    /* Display the result of the calculation */
    printf("The Arc Sine of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Sine of 0.500000 is 0.523599

atan() function in c programming language : -

atan() function in c helps you to return the arc tengent value of element(x).

Synatx : - 

double atan(double x);

atan () function in c will return the value between -π/2 and π/2.

atan() Example

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the atan of */
    value = 0.5;

    /* Calculate the Arc Tangent of value */
    result = atan(value);

    /* Display result for calculation */
    printf("Arc Tangent of %f is %f\n", value, result);

    return 0;
}

 out put : -

Arc Tangent of 0.500000 is 0.463648

atan2() function in c programming language : -

atan2() function in c slightly different form tan2() because return the arc tengent value of element(x/y).

Synatx : - 

double atan2(double y, double x);

atan2() function in c will return the value between -π and π.

atan2() Example : - 

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the two values we will find the atan2 of */
    value1 = 0.5;
    value2 = -0.5;

    /* Calculate the Arc Tangent of value1 and value2 */
    result = atan2(value1, value2);

    /* Display the result of the calculation */
    printf("The Arc Tangent of %f and %f is %f\n", value1, value2, result);

    return 0;
}

 Output : -

Arc Tangent of 0.500000 and -0.500000 is 2.356194

ceil() function in c programming language : -

ceil() function in c to return the smallest integer that is greater than or equal to define value(x).

Synatx : -  

double ceil(double x);

ceil() function in c return the smallest integer that is greater than or equal to define value x

 Example for ceil(): -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the ceil of */
    value = 1.6;

    /* Calculate the ceil of value */
    result = ceil(value);

    /* Display the result of the calculation */
    printf("The ceil of %f is %f\n", value, result);

    return 0;
}

 Output : -

ceil of 1.600000 is 2.000000

cos() function in c : -

cos() function in c return the cosine value of define value(x).

Synatx : - 

double cos(double x);

It returns the cosine value of x and measured in radians.
cos() example : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the cos of */
    value = 0.5;

    /* Calculate the Cosine of value */
    result = cos(value);

    /* Display the result of the calculation */
    printf("The Cosine of %f is %f\n", value, result);

    return 0;
}
 Out put  : - 
 
Cosine of 0.500000 is 0.877583

cosh() function in c programming: -

cosh() function in c return the hyperbolic cosine value of define value(x).

Synatx : - 

double cosh(double x);

 remember if magnitude of x is to large then it returns an error.

 Example cosh() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the cosh of */
    value = 0.5;

    /* Calculate the Hyperbolic Cosine of value */
    result = cosh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Cosine of %f is %f\n", value, result);

    return 0;
}

Output : -

The Hyperbolic Cosine of 0.500000 is 1.127626

exp() function in c programming: -

exp() function in c returns the e with raise the power x.

Synatx : -  

double exp(double x); 

 Example of exp() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the exp of */
    value = 5;

    /* Calculate the exponential of the value */
    result = exp(value);

    /* Display the result of the calculation */
    printf("The Exponential of %f is %f\n", value, result);

    return 0;
}

Output : -

Exponential of 2.100000 is 8.166170
 

fabs() function in c programming: -

fabs function in c programming helps to return the absolute value of a floating poin.

Synatx : - 

double fabs(double x);

 Example of fabs() : -


#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the fabs of */
    value = -2.1;

    /* Calculate the absolute value of value */
    result = fabs(value);

    /* Display the result of the calculation */
    printf("The Absolute Value of %f is %f\n", value, result);

    return 0;
}

Output : -

Absolute Value of -2.100000 is 2.100000

floor() function in c programming: -

floor() function in c programming is same as ceil() function but the only different is floor function return the largest interger that is smaller than or equal to x.

Synatx : -  

double floor(double x);

 Example of floor() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the floor of */
    value = 1.6;

    /* Calculate the floor of value */
    result = floor(value);

    /* Display the result of the calculation */
    printf("The floor of %f is %f\n", value, result);

    return 0;
}

 Output : -

floor of 1.600000 is 1.000000

fmod() function in c language: -

fmod() function in c returns the remainder value when x is divided by y.

Synatx : - 

double fmod(double x, double y);

 Example of fmod() : -

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the values we will find the fmod of */
    value1 = 1.6;
    value2 = 1.2;

    /* Calculate the remainder of value1 / value2 */
    result = fmod(value1, value2);

    /* Display the result of the calculation */
    printf("The fmod of %f and %f is %f\n", value1, value2, result);

    return 0;
}

 Output : -

fmod of 1.600000 and 1.200000 is 0.400000

frexp() function in c language: -

frexp() function in c programming will splites a floating-point value into a fraction and an exponent . Here fraction value is return by frexp function and exponent is stored by exp variable in c.

Synatx : - 

double frexp(double value, int *exp);

remember that fraction must be greater than or equal to 0.5 and less than 1. or it can be zero or must be equal to zero.

Examples of fexp() :-

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    int e;
    double f;

    /* Assign the value we will find the exp of */
    value = 1.5;

    /* Calculate the fraction and exponential of the value */
    f = frexp(value, &e);

    /* Display the result of the calculation */
    printf("The Fraction and Exponential of %f are %f and %d\n", value, f, e);

    return 0;
}

Output : -
 Fraction and Exponential of 1.500000 are 0.750000 and 1

idexp() function in c language: -

If you want to combine the fraction and an exponent into a floating-point value so idexp function in c is best for useing in c programming.

Synatx : -  

double ldexp(double fraction, int exp);

Examples of idexp() :-

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    int e;
    double result;

    /* Assign the value and exponential we will use to find the ldexp */
    value = 1.5;
    e = 2;

    /* Calculate the ldexp of the value and the exponential */
    result = ldexp(value, e);

    /* Display the result of the calculation */
    printf("The ldexp of %f with exponential %d is %f\n", value, e, result);

    return 0;
}

log() function in c programming language : -

log() function in c programming will help you in c program to find the logarithm value of x to the base of e.

Synatx : -

double log(double x);

Example of log()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will calculate the log of */
    value = 1.5;

    /* Calculate the log of the value */
    result = log(value);

    /* Display the result of the calculation */
    printf("The Natural Logarithm of %f is %f\n", value, result);

    return 0;
}

 Output : -

Natural Logarithm of 1.500000 is 0.405465

log10() function in c programming: -

log10() function in c programming will help you in c program to find the logarithm value of x to the base of 10. This function same as like log() function the only different is log10() function return the value with base 10 not with e.

Synatx : -

double log10(double x);

Example of log10()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will calculate the log10 of */
    value = 1.5;

    /* Calculate the log10 of the value */
    result = log10(value);

    /* Display the result of the calculation */
    printf("The Base 10 Logarithm of %f is %f\n", value, result);

    return 0;
}

 Output : -

Base 10 Logarithm of 1.500000 is 0.176091

log10() function in c programming: -

modf() function in c programming helps to splites floating point value into an integer and a fractional part. The fractional part return with modf() function in c programming and integer part is stored in the iptr variable

Synatx : - 

double modf(double value, double *iptr);

Example of modf()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double i, f;

    /* Assign the value we will calculate the modf of */
    value = 1.7;

    /* Calculate the modf of the value returning the fractional and integral parts */
    f = modf(value, &i);

    /* Display the result of the calculation */
    printf("The Integral and Fractional parts of %f are %f and %f\n", value, i, f);

    return 0;
}

 Output : - 

Integral and Fractional parts of 1.700000 are 1.000000 and 0.700000

    Some Common Function in c

 pow() function : -

 pow() function in c programming return the value of define element x which is raised to the power of y.

Synatx : -

double pow(double x, double y);

Example of pow()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value1, value2;
    double result;

    /* Assign the values we will use for the pow calculation */
    value1 = 4;
    value2 = 2;

    /* Calculate the result of value1 raised to the power of value2 */
    result = pow(value1, value2);

    /* Display the result of the calculation */
    printf("%f raised to the power of %f is %f\n", value1, value2, result);

    return 0;
}

 sin() function : -

 sin() function in c programming returns the sine of defined value x.

Synatx : -

double sin(double x);

Example of sin()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sin of */
    value = 0.5;

    /* Calculate the Sine of value */
    result = sin(value);

    /* Display the result of the calculation */
    printf("The Sine of %f is %f\n", value, result);

    return 0;
}

 sinh() function : -

 sinh() function in c programming returns the hyperbolic sine of defined value x.

Synatx : -

double sinh(double x);

Example of sinh()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sinh of */
    value = 0.5;

    /* Calculate the Hyperbolic Sine of value */
    result = sinh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Sine of %f is %f\n", value, result);

    return 0;
}

sqrt() function in c: -

sqrt() function in c programming return the square root of defined value x.

Synatx : -

double sqrt(double x);

Example of sqrt()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the sqrt of */
    value = 25;

    /* Calculate the square root of value */
    result = sqrt(value);

    /* Display the result of the calculation */
    printf("The Square Root of %f is %f\n", value, result);

    return 0;
}

 

tan() function in c: -

tan() function in c programming return the tangent of defined value x.

Synatx : -

double tan(double x);

Example of  tan()

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the tan of */
    value = 0.5;

    /* Calculate the Tangent of value */
    result = tan(value);

    /* Display the result of the calculation */
    printf("The Tangent of %f is %f\n", value, result);

    return 0;
}

 

tanh() function in c: -

tanh() function in c programming return the hyperbolic tangent of defined value x.

Synatx : -

double tanh(double x);

Example of  tanh()

 

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[])
{
    /* Define temporary variables */
    double value;
    double result;

    /* Assign the value we will find the tanh of */
    value = 0.5;

    /* Calculate the Hyperbolic Tangent of value */
    result = tanh(value);

    /* Display the result of the calculation */
    printf("The Hyperbolic Tangent of %f is %f\n", value, result);

    return 0;
}

 these all are the math function in c programming languages which helps you to reduce the length of a c program and also save time in c programming.

 

0 comments:

Post a Comment