C Programs Tutorials | IT Developer
IT Developer

C Programming - C Math Functions



Share with a Friend

C Programming - C Math Functions

Math Functions in C

C provides a variety of built-in mathematical functions in the math.h library. These functions cover a wide range of mathematical operations, including basic arithmetic, trigonometry, logarithmic functions, and more.

Common Mathematical Functions in C:

To use these functions, include the header file math.h:

C

#include <math.h>

  1. Basic Arithmetic Functions:
  • abs()
    Returns the absolute value of an integer.

C

int abs(int x);

Example:

C

#include <stdio.h>

#include <stdlib.h>

int main() {

    int x = -5;

    printf("Absolute value of %d is %d\n", x, abs(x));

    return 0;

}

  • fabs()
    Returns the absolute value of a floating-point number.

C

double fabs(double x);

Example:

C

#include <stdio.h>

#include <math.h>

int main() {

    double x = -5.6;

    printf("Absolute value of %.2f is %.2f\n", x, fabs(x));

    return 0;

}

  • floor()
    Returns the largest integer less than or equal to a given number.

C

double floor(double x);

Example:

C

#include <stdio.h>

#include <math.h>

int main() {

    double x = 3.7;

    printf("Floor of %.2f is %.2f\n", x, floor(x));

    return 0;

}

  • ceil()
    Returns the smallest integer greater than or equal to a given number.

C

double ceil(double x);

Example:

C

#include <stdio.h>

#include <math.h>

int main() {

    double x = 3.2;

    printf("Ceiling of %.2f is %.2f\n", x, ceil(x));

    return 0;

}

  • fmod()
    Returns the remainder of dividing x by y.

C

double fmod(double x, double y);

Example:

C

#include <stdio.h>

#include <math.h>

int main() {

    double x = 5.8, y = 2.3;

    printf("Remainder of %.2f / %.2f is %.2f\n", x, y, fmod(x, y));

    return 0;

}

  1. Trigonometric Functions:
  • sin()
    Returns the sine of an angle (in radians).

C

double sin(double x);

  • cos()
    Returns the cosine of an angle (in radians).

C

double cos(double x);

  • tan()
    Returns the tangent of an angle (in radians).

C

double tan(double x);

  • asin()
    Returns the arcsine (inverse sine) of a value, in radians.

C

double asin(double x);

  • acos()
    Returns the arccosine (inverse cosine) of a value, in radians.

C

double acos(double x);

  • atan()
    Returns the arctangent (inverse tangent) of a value, in radians.

C

double atan(double x);

  • atan2()
    Returns the arctangent of y/x using the signs of both arguments to determine the correct quadrant.

C

double atan2(double y, double x);

  1. Exponential and Logarithmic Functions:
  • exp()
    Returns the exponential function of a given number (e^x).

C

double exp(double x);

  • log()
    Returns the natural logarithm (base e) of a number.

C

double log(double x);

  • log10()
    Returns the logarithm of a number with base 10.

C

double log10(double x);

  • pow()
    Returns x raised to the power of y (i.e., x^y).

C

double pow(double x, double y);

  • sqrt()
    Returns the square root of a number.

C

double sqrt(double x);

  1. Hyperbolic Functions:
  • sinh()
    Returns the hyperbolic sine of a value.

C

double sinh(double x);

  • cosh()
    Returns the hyperbolic cosine of a value.

C

double cosh(double x);

  • tanh()
    Returns the hyperbolic tangent of a value.

C

double tanh(double x);

  1. Miscellaneous Functions:
  • gcd()
    Returns the greatest common divisor of two integers.

C

int gcd(int x, int y);

  • lcm()
    Returns the least common multiple of two integers.

C

int lcm(int x, int y);

  • modf()
    Breaks a floating-point number into its integer and fractional parts.

C

double modf(double x, double *intpart);

Example Program Using Math Functions:

C

#include <stdio.h>

#include <math.h>

int main() {

    double x = 2.5;

    double y = 3.5;

    // Basic Math Operations

    printf("Absolute value of %.2f is %.2f\n", x, fabs(x));

    printf("Floor of %.2f is %.2f\n", x, floor(x));

    printf("Ceiling of %.2f is %.2f\n", y, ceil(y));

    // Exponential and Logarithmic

    printf("Exponent of %.2f is %.2f\n", x, exp(x));

    printf("Logarithm of %.2f is %.2f\n", x, log(x));

    // Trigonometric

    printf("Sine of %.2f is %.2f\n", x, sin(x));

    printf("Cosine of %.2f is %.2f\n", y, cos(y));

    // Power

    printf("%.2f raised to the power %.2f is %.2f\n", x, y, pow(x, y));

    return 0;

}

Conclusion:

The math.h library in C provides a wide range of functions for performing mathematical operations. Whether you are working with basic arithmetic, trigonometry, logarithms, or advanced hyperbolic functions, this library makes it easy to perform complex mathematical calculations in your programs.