Advanced Maths Functions

Here's a list of important math functions in the C standard library along with sample code and explanations:

  1. sqrt() - Square Root:

    • Calculates the square root of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 25.0;
        double result = sqrt(num);
        printf("Square root of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. pow() - Power:

    • Calculates the power of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double base = 2.0;
        double exponent = 3.0;
        double result = pow(base, exponent);
        printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent, result);
        return 0;
    }
  1. fabs() - Absolute Value:

    • Returns the absolute value of a floating-point number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = -7.5;
        double result = fabs(num);
        printf("Absolute value of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. ceil() - Ceiling:

    • Rounds up to the nearest integer.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 4.3;
        double result = ceil(num);
        printf("Ceiling of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. floor() - Floor:

    • Rounds down to the nearest integer.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 4.7;
        double result = floor(num);
        printf("Floor of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. sin(), cos(), tan() - Trigonometric Functions:

    • Calculate sine, cosine, and tangent of an angle (in radians).
    #include <stdio.h>
    #include <math.h>

    int main() {
        double angle = 30.0;
        double radian = angle * (M_PI / 180.0);
        printf("sin(%.2f) = %.2f\n", angle, sin(radian));
        printf("cos(%.2f) = %.2f\n", angle, cos(radian));
        printf("tan(%.2f) = %.2f\n", angle, tan(radian));
        return 0;
    }
  1. log() - Natural Logarithm:

    • Calculates the natural logarithm of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 2.0;
        double result = log(num);
        printf("Natural log of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. exp() - Exponential Function:

    • Calculates the exponential function (e^x).
    #include <stdio.h>
    #include <math.h>

    int main() {
        double exponent = 2.0;
        double result = exp(exponent);
        printf("e raised to the power of %.2f is %.2f\n", exponent, result);
        return 0;
    }

These are just a few examples of the many math functions available in the C standard library. Make sure to include the <math.h> header in your C program to use these functions.